Brett
Brett

Reputation: 1837

navigationBarTitle with variable gives various random errors

Does anyone know why I get errors when using a variable for my navigationBarTitle ? The error changes depending on where I put this line of code, or what the main container is. Currently I get the usual 'CGFloat' is not convertible to 'CGFloat?'

But if I swap the navigationBarTitle with the padding then I get Ambiguous reference to member 'navigationBarTitle(_:displayMode:)'

If I swap the sectionName for "" then the compiler is happy.

struct TestView : View {

    private var sectionName = "Hello"

    var body : some View {

        VStack(alignment: .leading) {

            Text("Hello")
        }
        .navigationBarTitle(sectionName, displayMode:.inline)
        .padding(.horizontal, 12.0)
    }
}

Upvotes: 1

Views: 85

Answers (1)

Hrabovskyi Oleksandr
Hrabovskyi Oleksandr

Reputation: 3275

First, to see the real error you may update Xcode to 11.4 version - it shows errors better. The second - if you will go to SwiftUI framework, you will see:

public func navigationBarTitle(_ titleKey: LocalizedStringKey, displayMode: NavigationBarItem.TitleDisplayMode) -> some View

so, the error is that you use String instead of LocalizedStringKey

the solution is:

// replace this
//private var sectionName = "Hello" 
// to this:
private var sectionName: LocalizedStringKey = "Hello"

Upvotes: 1

Related Questions