Post

Replies

Boosts

Views

Activity

SwiftUI date formatter uses wrong locale in navigationTitle?
(I've posted more details of my question on Stack Overflow: https://stackoverflow.com/q/64566387/23649) This simple SwifUI code produces a strange result: the content view shows "October 27, 2020", while the navigation title shows "2020 M10 27". I'm using Xcode 12.1 and the iOS 14.1 simulator. The same happens on my device running 14.1. struct ContentView: View { 	var body: some View { 		NavigationView { 			Text(Date(), style: .date) 				.navigationTitle( 					Text(Date(), style: .date) 				) 		} 	} } I tried this, but got the same result: let formatter = DateFormatter() formatter.dateStyle = .long Text(Date(), formatter: formatter) Finally, pre-formatting the text seems to give the expected results: Text(formatter.string(from: Date())) I tried to debug by subclassing DateFormatter and overriding string(for obj: Any?). I found that for the title bar, the formatter's locale is set to " (fixed)", i.e. an empty string as the locale identifier. When the content view is being displayed, the formatter's locale is set to en_US as I'd expect in my system locale. I tried wrapping my views with .environment(\.locale, Locale.current), but that didn't help. Is this just a bug? Do I really need to pre-format my dates as a workaround?
2
0
1.7k
Oct ’20