I am having a hard time trying to figured out how to focus on a specific cell/row in the list in the SwiftUI 2.0 and tvOS 14. I need to be able to focus and select a specific record when I am navigated to a view. However when the focus is switched to the list, some random row is focused. I've tried ScrollView and List to create a list of items with Buttons as items and with appropriate prefersDefaultFocus. Nothing works. Here's some sample code:
Any pointers?
Code Block struct ChannelListView: View { @Namespace private var namespace @ObservedObject var viewModel : LiveViewModel @State var selection = Set<ChannelItem>() var body: some View { List(viewModel.channels, selection: $selection){ item in ScrollViewReader { proxy in Button(action: { }){ ChannelItemView(item: item, selectedItem: $viewModel.selectedChannel, onSelected: { id in }) .padding(.vertical, 2) } .buttonStyle(ChannelButtonStyle()) .prefersDefaultFocus(item == viewModel.selectedChannel, in: namespace) } } .focusScope(namespace) } }
Any pointers?