skrew
skrew

Reputation: 889

SwiftUI and tvOS (focusable)

I know, SwiftUI is currently in beta, but i want to be sure to use focusable correctly.

Actually, when i scroll down in the list, the focused item skips 3 or 4 rows when the list scroll down (up scroll direction is fine).

You can try with this code, in b4 version: (Edit: same for b5)

import SwiftUI

struct TestList: Identifiable {
    var id: Int
    var name: String
}

let testData = [Int](0..<50).map { TestList(id: $0, name: "Row \($0)")  }


struct SwiftUIView : View {
    var testList: [TestList]

    var body: some View {
        List {
            ForEach(testList) { txt in
                TestRow(row: txt)
            }
        }
    }
}

struct TestRow: View {
    var row: TestList

    @State private var backgroundColor = Color.clear

    var body: some View {
        Text(row.name)
        .focusable(true) { isFocused in
            self.backgroundColor = isFocused ? Color.green : Color.blue
            if isFocused {
                print(self.row.name)
            }
        }
        .background(self.backgroundColor)
    }
}

#if DEBUG
struct SwiftUIView_Previews : PreviewProvider {
    static var previews: some View {
        SwiftUIView(testList: testData)
    }
}
#endif

Thanks.

Upvotes: 4

Views: 2548

Answers (1)

skrew
skrew

Reputation: 889

It finally working for me with Xcode 11 GM...

Upvotes: -1

Related Questions