I am really struggling with Toolbar button issues. I have a fairly complicated app that uses a lot of Toolbar items throughout. Most work as expected.
However, there are two situations that are extremely frustrating. The first concerns placing a toolbar and button on the keyboard for a particular view. The app uses NavigationSplitView. In the main list I have a ToolbarItem that contains a menu to navigate to a screen to add an Item (a SwiftData model), and four other destinations(.topBarTrailing). This all works as expected most of the time. Occasionally the ToolbarItem will simply be missing. I need to hard close the app and reopen to be able to do anything.
Worse is my attempt to add a ToolbarItem to the keyboard. I have an @State var Bool to move to the screen to add a new Item and I want to add a ToolbarItem for that Add Item view to include a keyboard toolbar button to move through focus fields and a second keyboard toolbar button to dismiss the keyboard. If I use .fullScreenCover(isPresented:) the keyboard toolbar does not appear at all. If I use .sheet(isPresented:) the keyboard toolbar appears, the Dismiss button works but the Next navigation for changing focus fields does nothing at all.
I wrote a simple one view app with the same functionality and it works as expected. Here is the code:
struct KeyboardToolbarView: View {
@Environment(.dismiss) var dismiss
@State private var name: String = ""
@State private var subject: String = ""
@State private var count: String = ""
@State private var value: String = ""
@FocusState private var focusedField: Field?
enum Field: Hashable {
case name, subject, count, value
}
var body: some View {
NavigationStack {
VStack {
VStack {
Form {
TextField("Name", text: $name)
.focused($focusedField, equals: .name)
TextField("Subject", text: $subject, axis: .vertical)
.focused($focusedField, equals: .subject)
TextField("Count", text: $count)
.keyboardType(.numberPad)
.focused($focusedField, equals: .count)
TextField("Value", text: $value)
.keyboardType(.decimalPad)
.focused($focusedField, equals: .value)
}//form
}
Spacer()
Button {
dismiss()
} label: {
Text("Dismiss")
}
}
.textFieldStyle(.roundedBorder)
.padding()
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Button("Next") {
moveToNextField()
}
Button("Done") {
hideKeyboard()
}
}
}//toolbar
}//nav
}//body
private func moveToNextField() {
switch focusedField {
case .name:
focusedField = .subject
case .subject:
focusedField = .count
case .count:
focusedField = .value
default:
hideKeyboard()
}
}
private func hideKeyboard() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
If I add this exact view to my app and navigate to it from the main menu with .sheet(isPresented:) the functionality is as expected. If I navigate to it with .fullScreenCover(isPresented:) the keyboard toolbar does not appear at all.
For the most part – ALL of my screens have buttons in one or both .topBarLeading or .topBarTrailing. All work as expected with the exception of the quirk described above for the menu button (once in a while it does not appear).
Here is a ContentView that can demonstrate the menu system. Tap the menu then select a TextField – no keyboard toolbar. Sometimes typing in a field will raise the keyboard toolbar. Change the .fullScreenCover to .sheet and it usually works. In my full app it is even more erratic.
struct ContentView: View {
@State private var showKeyboardToolbarView: Bool = false
var body: some View {
NavigationSplitView {
VStack {
Text("Sidebar View")
List {
Text("One")
Text("Two")
Text("Three")
}
.fullScreenCover(isPresented: $showKeyboardToolbarView) {
KeyboardToolbarView()
}
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Menu {
Button(action: {
showKeyboardToolbarView = true
}, label: {
Label("Add New Item", systemImage: "plus")
})
} label: {//menu
Image(systemName: "ellipsis.circle")
.font(.system(size: 20))
}
}
}
} detail: {
Text("Detail View")
}
}
}
I would really appreciate some guidance. I’ve been using toolbar ToolbarItem and ToolbarItemGroup for a long time with no issues. Something changed. Xcode 15.4 iOS 17.5
5