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?
Edit:
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)
}
}
2
Answers
If you look closely at what the
navigationBarTitle
call resolves to, it isn’t the method you linked:It is:
It can’t resolve to the first one because you are not passing a
Text
as the title, are you? You declared the argumenttitle
to be aString
.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 ofnavigationBarTitle
:LocalizedStringKey
conforms toExpressibleByStringLiteral
, so string literals can be passed to an argument of typeLocalizedStringKey
, but not a variable liketitle
.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)