Saturday, November 21, 2026
HomeiOS Developmentios - How do I present totally different buttons within the toolbar...

ios – How do I present totally different buttons within the toolbar based mostly on which subject is targeted in SwiftUI?

[ad_1]

I need to present totally different buttons based mostly on which subject is targeted, however the focus state is all the time one behind. Right here is the code:

struct ContentView: View {
    enum FocusField {
        case one
        case two
    }
    
    @FocusState var centered: FocusField?
    
    var physique: some View {
        VStack {
            TextField("One", textual content: .fixed(""))
                .centered($centered, equals: .one)
            TextField("Two", textual content: .fixed(""))
                .centered($centered, equals: .two)
        }
        .toolbar {
            ToolbarItemGroup(placement: .keyboard) {
                if centered == .one {
                    Textual content("One")
                } else if centered == .two {
                    Textual content("Two")
                } else {
                    Textual content("None")
                }
                Spacer()
                Button("Carried out") { centered = nil }
            }
        }
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments