admin管理员组

文章数量:1402828

I am building a SwiftUI tvOS app with a LazyVGrid using .adaptive(minimum: 300) columns. The focus behavior works fine for most items, but when an item (like item-9) does not have another item directly below it, the downward movement gets stuck.

Here’s an example of my UI:

Current Focus: item-9 (Highlighted in Red) Goal: When swiping down, I expect the focus to move to item-12, the nearest available item below. Issue: The focus stays stuck on item-9 instead of moving down.

Here is my current code:

struct AdaptiveLazyHGridView: View {
    let items = Array(1...22)
    var adaptiveColumns = [GridItem(.adaptive(minimum: 300.0), spacing: 10)]

    var body: some View {
        VStack{
            ScrollView(.vertical) {
                LazyVGrid(columns: adaptiveColumns) {
                    ForEach(0 ..< 13) { item in
                        Button(action: {
                            
                        }) {
                            ZStack{
                                FocusableRectangle()
                                Text("item - \(item)")
                            }
                        }.buttonStyle(CardButtonStyle())
                    }
                }.padding(40)
            }
        }
    }
}
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) {
                color = isFocused ? Color.red : Color.blue
            }
    }
}

本文标签: iosSwiftUI tvOS LazyVGrid Focus Navigation Issue Can39t Move Down When No Direct Below ItemStack Overflow