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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.