I lowered my minimum deployment target from iOS 14.0 to iOS 13.0. I then got this error:
'navigationBarTitle(_:displayMode:)' is only available in iOS 14.0 or newer
But, the documentation says it's available from iOS 13.0-14.2, and previous SO answers posted (back when iOS 13 was the newest) also used it.
Is it because it's "Deprecated" that I can't use it? But then why can I use it in iOS 14.0?
After trying to reproduce, it seems to work only if I don't have title
stored in a property.
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hi!")
.navigationBarTitle("Title", displayMode: .inline)
}
}
struct DetailView: View {
var title: String = ""
var body: some View {
Text("Hi!")
.navigationBarTitle(title, displayMode: .inline)
}
}
If you look closely at what the navigationBarTitle
call resolves to, it isn't the method you linked:
func navigationBarTitle(_ title: Text, displayMode: NavigationBarItem.TitleDisplayMode) -> some View
It is:
func navigationBarTitle<S>(_ title: S, displayMode: NavigationBarItem.TitleDisplayMode) -> some View where S : StringProtocol
It can't resolve to the first one because you are not passing a Text
as the title, are you? You declared the argument title
to be a String
.
Here's the documentation for the second method. From the documentation we can clearly see that it is available from iOS 14.0 to 14.2.
The reason why using a string literal as the title works (such as in ContentView
) is because you are calling yet another overload of navigationBarTitle
:
func navigationBarTitle(_ titleKey: LocalizedStringKey, displayMode: NavigationBarItem.TitleDisplayMode) -> some View
LocalizedStringKey
conforms to ExpressibleByStringLiteral
, so string literals can be passed to an argument of type LocalizedStringKey
, but not a variable like title
.
Its because you are using a string for your title instead of a Text view.
I ran into this same issue as you.
Basically in iOS 14 they added the ability to just drop a string into your nav bar directly. Prior to that you had to use .navigationBarTitle(Text(yourString), displayMode: .inline)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With