UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: iOS16+ TextField, focus and toolbar button placement .keyboard issues

Forums > SwiftUI

I have TextFields in my Form that have focused set and I switch to the next Field onCommit. However one TextField is a TextEditor and then I can't commit. Any way around this and have the user commit the text and set focus to the nextfield?

✅ I tried to workaround it by having a button on the keyboard, but.... ...this only shows when embedded in a navigationView. Is that correct? (cause I don't actually need a navigationView in this View). EDIT: Confirmed, keyboard button needs a NavigationView

✅ Also, when I adding a spacer in HStack for the button to have it sit at the trailing edge, the button just doesn't show. (Off screen ?)

                    .toolbar {
                        ToolbarItem(placement: .keyboard) {
                            HStack {
                                // Spacer()
                                Button {
                                    setNextFocus()
                                } label: {
                                    Image(systemName: "keyboard.chevron.compact.down")
                                }
                            }
                        }
                    }
    func setNextFocus() {
        switch focus {
        case \Self.title:
            focus = \Self.platform
        case \Self.platform:
            focus = \Self.year
        case \Self.year:
            focus = \Self.comments
        case \Self.comments:
            focus = \Self.genre
        case \Self.genre:
            focus = \Self.publisher
        case \Self.publisher:
            focus = \Self.developer
        default:
            focus = \Self.title
        }
    }

1      

I solved the spacer() issue by replacing it with an EmptyView() and frame modifiers. Don't knbow why this works, but it does!

                    .toolbar {
                        ToolbarItem(placement: .keyboard) {
                            HStack {
                                EmptyView()
                                    .frame(maxWidth: .infinity, alignment: .leading)

                                Button {
                                    setNextFocus()
                                } label: {
                                    Image(systemName: "keyboard.chevron.compact.down")
                                }
                            }
                            .frame(maxWidth: .infinity, alignment: .trailing)
                        }
                    }

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.