Manually Handling Focus State in tvOS 14: Alternatives to @FocusState and .focusable

125 Views Asked by At

How can we manually handle focus state in tvOS 14, since the @FocusState property wrapper and .focusable modifier are only available from tvOS 15 onwards? Are there any alternative approaches or best practices to handle focus state in tvOS 14?

struct MyView: View, Equatable {
    
    @FocusState var isFocused: Bool
    
    @State var screenSize: CGSize
    @Binding var selectedfield: MenuFields
    var menuAction: ((MenuFields) -> Void)
    
    @State var isMenufocused: Bool = false
    
    var body: some View {
        // Menu View
        ZStack {
            HStack {
                Spacer().frame(width: Constants.spacerWidth)
                MenuStackView(isMenufocused: $isMenufocused, selectedfield: $selectedfield, menuAction: { field in
                    menuAction(field)
                })
                .frame(width: Constants.menStackViewWidth)
                .focused($isFocused)
                .onChange(of: isFocused) { newValue in
                    print("MyMenu", newValue)
                    isMenufocused = newValue
                }
                Spacer()
            }
        }
        .focusSection()
    }
}
0

There are 0 best solutions below