Search code examples
macosswiftuitab-ordering

How do you fix reversed tab order in SwiftUI for MacOS? (key view loop)


I have a window with some text fields and when tabbing between fields, focus moves from the bottom to top instead of top-down. How do you make it tab top-to-bottom?

var body: some View {
  VStack {
    TextField("First name", text: $firstName)
      .modifier(InputModifier())
    TextField("Last namee", text: $lastName)
      .modifier(InputModifier())
    })
  }
}

Solution

  • Add .focusable() to the views and they will tab top-to-bottom.

    var body: some View {
      VStack {
        TextField("First name", text: $firstName)
          .modifier(InputModifier())
          .focusable()
        TextField("Last name", text: $lastName)
          .modifier(InputModifier())
          .focusable()
        })
      }
    }