Thursday, September 11, 2025
HomeiOS Developmentios - SwiftUI Toolbar Buttons Do not Seem or Do not Work

ios – SwiftUI Toolbar Buttons Do not Seem or Do not Work


I’m actually scuffling with Toolbar button points. I’ve a reasonably sophisticated app that makes use of plenty of Toolbar objects all through. Most work as anticipated.

Nonetheless, there are two conditions which can be extraordinarily irritating. The primary considerations inserting a toolbar and button on the keyboard for a specific view. The app makes use of NavigationSplitView. In the primary record I’ve a ToolbarItem that comprises a menu to navigate to a display screen so as to add an Merchandise (a SwiftData mannequin), and 4 different locations(.topBarTrailing). This all works as anticipated more often than not. Often the ToolbarItem will merely be lacking. I must arduous shut the app and reopen to have the ability to do something.

Worse is my try so as to add a ToolbarItem to the keyboard. I’ve an @State var Bool to maneuver to the display screen so as to add a brand new Merchandise and I need to add a ToolbarItem for that Add Merchandise view to incorporate a keyboard toolbar button to maneuver via focus fields and a second keyboard toolbar button to dismiss the keyboard. If I take advantage of .fullScreenCover(isPresented:) the keyboard toolbar doesn’t seem in any respect. If I take advantage of .sheet(isPresented:) the keyboard toolbar seems, the Dismiss button works however the Subsequent navigation for altering focus fields does nothing in any respect.

I wrote a easy one view app with the identical performance and it really works as anticipated. Right here is the code:

struct KeyboardToolbarView: View {
    
    @Setting(.dismiss) var dismiss
    
    @State personal var title: String = ""
    @State personal var topic: String = ""
    @State personal var rely: String = ""
    @State personal var worth: String = ""
    
    @FocusState personal var focusedField: Discipline?
    
    enum Discipline: Hashable {
        case title, topic, rely, worth
    }
    
    var physique: some View {
        NavigationStack {
            VStack {
                VStack {
                    Type {
                        TextField("Identify", textual content: $title)
                            .centered($focusedField, equals: .title)
                        
                        TextField("Topic", textual content: $topic, axis: .vertical)
                            .centered($focusedField, equals: .topic)
                        
                        TextField("Rely", textual content: $rely)
                            .keyboardType(.numberPad)
                            .centered($focusedField, equals: .rely)
                        
                        TextField("Worth", textual content: $worth)
                            .keyboardType(.decimalPad)
                            .centered($focusedField, equals: .worth)
                    }//type
                }
                Spacer()
                Button {
                    dismiss()
                } label: {
                    Textual content("Dismiss")
                }

            }
            .textFieldStyle(.roundedBorder)
            .padding()
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Button("Subsequent") {
                        moveToNextField()
                    }
                    Button("Carried out") {
                        hideKeyboard()
                    }
                }
            }//toolbar
        }//nav
    }//physique
    
    personal func moveToNextField() {
        change focusedField {
        case .title:
            focusedField = .topic
        case .topic:
            focusedField = .rely
        case .rely:
            focusedField = .worth
        default:
            hideKeyboard()
        }
    }
    
    personal func hideKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

If I add this precise view to my app and navigate to it from the primary menu with .sheet(isPresented:) the performance is as anticipated. If I navigate to it with .fullScreenCover(isPresented:) the keyboard toolbar doesn’t seem in any respect.

For probably the most half – ALL of my screens have buttons in a single or each .topBarLeading or .topBarTrailing. All work as anticipated apart from the quirk described above for the menu button (every now and then it doesn’t seem).

I might actually admire some steering. I have been utilizing toolbar ToolbarItem and ToolbarItemGroup for a very long time with no points. One thing modified. Xcode 15.4 iOS 17.5

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments