I have multiple scrollable horizontal list in a global vertical list and I have difficulties to manage focus change.
I have two major issue :
When a view don't have another view below, the bottom gesture don't update focus. I would like the focus to move to the near below view. In my GIF example, from fourth view in Section 1 to Section 2 second view.
When an view don't have an another view directly below, the bottom gesture move focus to the view that is vertically below. I would like the focus to move to the nearest bottom view. In my example from Section 1 third view to Section 2 second view instead of Section 3 third view.
I have many example of this issue in my tvOS app that I have fixed by using the combination of @Environment(\.resetFocus) var resetFocus and the prefersDefaultFocus viewModifier but I'm struggling with this use case.
I have also see another similar use case on this question but there is no answer yet.
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
Text("Section 1")
ScrollView(.horizontal) {
HStack() {
ForEach(0 ..< 10) { item in
Button(action: {
}) {
FocusableRectangle()
}.buttonStyle(CardButtonStyle())
}
}.padding(40)
}
Text("Section 2")
ScrollView(.horizontal) {
HStack() {
ForEach(0 ..< 2) { item in
Button(action: {
}) {
FocusableRectangle()
}.buttonStyle(CardButtonStyle())
}
}.padding(40)
}
Text("Section 3")
ScrollView(.horizontal) {
HStack() {
ForEach(0 ..< 3) { item in
Button(action: {
}) {
FocusableRectangle()
}.buttonStyle(CardButtonStyle())
}
}.padding(40)
}
Spacer()
}
}
}
struct FocusableRectangle: View {
@Environment(\.isFocused) var isFocused: Bool
@State var color = Color.blue
var body: some View {
Rectangle()
.fill(color)
.frame(width: 300.0, height: 200.0)
.onChange(of: isFocused, perform: { value in
color = value ? Color.red : Color.blue
})
}
}
