A ToolbarItem
with navigationBar*
placement disappears when placed in a toolbar
on a detail
view of NavigationSplitView
.
Expectation: The toolbar item doesn't disappear after device rotation.
Reality: The toolbar item disappears after device rotation.
Reproducible on iPhone 15 Pro Max simulator running 17.4
import SwiftUI
@main
struct MyApp: App {
@State private var selection: Int?
@State private var visibility: NavigationSplitViewVisibility = .detailOnly
var body: some Scene {
WindowGroup {
NavigationSplitView(columnVisibility: $visibility) {
List(1..<10, selection: $selection) { number in
NavigationLink(value: number) {
Button(number.description) {
selection = number
}
}
}
} detail: {
if let selection {
LinearGradient(colors: [.blue, .black], startPoint: .top, endPoint: .bottom)
.navigationTitle(selection.description)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Text("A button")
}
}
} else {
Text("Please make a selection")
}
}
}
}
}
The problem doesn't exhibit itself when using a NavigationStack
instead.
import SwiftUI
@main
struct MyApp: App {
@State private var selection: Int?
var body: some Scene {
WindowGroup {
NavigationStack {
List(1..<10) { number in
NavigationLink(value: number) {
Button(number.description) {
selection = number
}
}
}
.navigationDestination(for: Int.self) { selection in
LinearGradient(colors: [.blue, .black], startPoint: .top, endPoint: .bottom)
.navigationTitle(selection.description)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Text("A button")
}
}
}
}
}
}
}