9

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

'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.

comparison of storing "title" inside a property or not

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)
    }
}
aheze
  • 24,434
  • 8
  • 68
  • 125
  • I think: `navigationBarTitle()` has been deprecated and replaced by `.navigationTitle()` – Torewin Nov 06 '20 at 03:16
  • Which Xcode version do you use? – Asperi Nov 06 '20 at 03:53
  • @Asperi 12.1 (12A7403). – aheze Nov 06 '20 at 03:54
  • @Sweeper **1.** Create new SwiftUI project, **2.** Lower min deployment target to 13.0, **3.** Replace `ContentView` with `struct ContentView: View { var title: String = ""; var body: some View { Text("Hello, world!").navigationBarTitle(title, displayMode: .inline) } }` – aheze Nov 06 '20 at 04:03
  • Huh, seems to work if I remove the `title` property. The error comes back when I add it. See https://imgur.com/a/rfRCGYn. I edited my post – aheze Nov 06 '20 at 04:06

2 Answers2

16

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:

enter image description here

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.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
12

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)

Xaxxus
  • 1,083
  • 12
  • 19