Post

Replies

Boosts

Views

Activity

SwiftUI: .prefersDefaultFocus with TabView doesn't work
I am developing a tvOS application and I wanted to use a view like split-screen of course I need focus management in it. I've seen a video from WWDC and my research that this can be done with the prefersDefaultFocus(_:in:)modifier. I even made a little demo. While it works fine if TabView is not used on the screen, prefersDefaultFocus(_:in:) does not work when TabView is added. Have any of you encountered this situation? How can it be overcome? Here is my code sample, you can test both cases; @main struct MyApp: App { var body: some Scene { WindowGroup { //ContentView() ContentWithTabView() } } } struct ContentView: View { @Environment(\.resetFocus) var resetFocus @Namespace private var namespace var body: some View { HStack(spacing: 100) { Group { VStack { Button ("1") {} .prefersDefaultFocus(in: namespace) Button ("2") {} Button ("3") {} }} Group{ VStack { Button ("1") {} Button ("2") {} Button ("3") {} Button ("Reset to default focus") { resetFocus(in: namespace) } } } } .focusScope(namespace) } } struct ContentWithTabView: View { var body: some View { NavigationView { TabView { ContentView() .tabItem { Image(systemName: "house.fill") Text("Home") } ContentView() .tabItem { Image(systemName: "house.fill") Text("Home") } } } } }
0
0
855
Jan ’22