SwiftUI NavigationView navigationBarTitle LayoutConstraints issue
NavigationBarTitle is deprecated from iOS 14.3 https://developer.apple.com/documentation/swiftui/view/navigationbartitle(_:displaymode:)-8buvp
You can use NavigationTitle and .navigationViewStyle(StackNavigationViewStyle())
import SwiftUI
struct HomeView: View {
var body: some View {
NavigationView {
Text("Text")
.navigationBarTitle("Hey there", displayMode: .inline)
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
NavigationBarTitle
is deprecated from iOS 14.3
.
However if you still wish to use it, try to add
.navigationViewStyle(StackNavigationViewStyle())
on the navigationView
that will fix the warning.
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Hello, world!")
.padding()
Spacer()
}
.navigationBarTitle("Hey there", displayMode: .inline)
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
The new way:
.navigationBarTitleDisplayMode(.inline)
.toolbar(content: {
ToolbarItem(placement: .principal, content: {
Text("Title")
})})
It had nothing to do with NavigationBarTitle
. .navigationTitle
is not deprecated. Seems the whole issue is mainly with the new Xcode 12.4 update, even for simple code:
var body: some View {
NavigationView{
Text("Hopefully will work this time")
.navigationTitle("Error with constains")
}
}
adding .navigationViewStyle(StackNavigationViewStyle())
fixes problem
var body: some View {
NavigationView{
Text("Yes it does!")
.navigationTitle("Wow it works")
}
.navigationViewStyle(StackNavigationViewStyle())
}
Xcode 13.0 Beta 1
This is still causing a contraint error unless you add the StackNavigationViewStyle modifier.
However, the documentation for StackNavigationViewStyle
states:
Do not use this type directly. Instead, use stack.
var body: some View {
NavigationView {
Text("Text")
.navigationTitle("Title")
}
.navigationViewStyle(.stack)
}