I am building a tvOS app which divides the screen into two parts horizontally. Both sides have buttons. I want a certain button on the right side to have focus. To achieve that I am marking the enclosing HStack
with .focusScope()
and then use .prefersDefaultFocus(in:)
on the button which should have focus.
This works fine, even if I add another focus scope inside (see source code). But as soon as I wrap the HStack
in a NavigationView
it falls apart. The scope is not set as intended, instead the left view always gets the focus.
Am I missing something? Is this intended? I’m a little lost right now. I am targeting tvOS 16.4 and using Xcode 14.3.1.
Fun fact: The simulator view inside Xcode displays the view correctly. The simulator and an actual device are not.
Here is an example to showcase that:
struct ContentView: View {
@Namespace private var mainNamespace
@Namespace private var rightNamespace
var body: some View {
// NavigationView {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
HStack {
VStack {
Button("left") {}
}
VStack {
Button("right 1") {}
Button("right 2") {}
.prefersDefaultFocus(in: rightNamespace)
}
.focusScope(rightNamespace)
.prefersDefaultFocus(in: mainNamespace)
}
.focusScope(mainNamespace)
.padding(.top, 50)
}
.padding()
// }
}
}