r/SwiftUI • • 6d ago

Question iOS 27 changed textfield behavior in toolbars, how can I extend its width?

Post image

In iOs 26, with the exact same code, the controls at the bottom automatically went edge to edge on the bottom of the screen, but on 27, the textfield only extends to the width of the placeholder text inside of it. I can't figure out for the life of me how to get it to extend its width again! Any help would be very appreciated.

Code:

ToolbarItemGroup(placement: .bottomBar) {

TextField("Add Player", text: $newPlayer)

.font(.body)

.frame(maxWidth: .infinity)

.focused($isFocused)

.onSubmit(addPlayer)

.padding(.horizontal)

Button ("Add", systemImage: "plus") {

addPlayer()

}

.disabled(newPlayer.isEmpty || nextGamePlayers.contains(newPlayer))

.sensoryFeedback(.selection, trigger: nextGamePlayers)

Button("Play", systemImage: "play.fill", role: .confirm) {

isFocused = false

withAnimation {

menuShowing = false

gameSetupShowing = false

}

onEndGame?()

onStartGame?()

}

.sensoryFeedback(.selection, trigger: gameActive)

.disabled(nextGamePlayers.count < 2)

}

6 Upvotes

5 comments sorted by

2

u/No_Pen_3825 5d ago

Honestly I’d take it out of the toolbar and put it at the end of the players section like maps does with add stop

1

u/ducky_duck666 5d ago

i had it like that before, it kept creating a weird bug where it would add 2 of the same player but one wasn’t really listed in the code

1

u/No_Pen_3825 5d ago

So… fix the bug?

1

u/ThanhDongNguyen 6d ago

I think in 27 the toolbar sizes items by their ideal size, so .frame(maxWidth: .infinity) inside ToolbarItemGroup just gets ignored. The bottom bar doesn't give the item extra space to grow anymore.

For an input row like this I would take it out of the toolbar and use safeAreaBar (26+, or safeAreaInset if you support older):

.safeAreaBar(edge: .bottom) {
    HStack {
        TextField("Add Player", text: $newPlayer)
            .focused($isFocused)
            .onSubmit(addPlayer)
            .padding(.horizontal, 12)
            .padding(.vertical, 10)
            .glassEffect(.regular.interactive(), in: .capsule)

        Button("Add", systemImage: "plus", action: addPlayer)
            .labelStyle(.iconOnly)
            .buttonStyle(.glass)
            .disabled(newPlayer.isEmpty || nextGamePlayers.contains(newPlayer))

        Button("Play", systemImage: "play.fill") {
            // your existing play code
        }
        .labelStyle(.iconOnly)
        .buttonStyle(.glassProminent)
    }
    .padding(.horizontal)
}

Inside a normal HStack the TextField takes all the leftover width, and the bar still moves up with the keyboard. You also don't depend on how Apple changes toolbar layout again next year.