[英]SwiftUI tvOS Button/NavigationLink .focusable()
我有一个直接的跨平台 SwiftUI 项目。 我很难在 tvOS 上使用NavigationLink
。
(以下均为伪代码,可能有错别字)
以这个为例,这很好用:
struct ContentView: View {
var body: some View {
NavigationView {
ScrollView(.horizontal) {
HStack {
ForEach(items) { item in
NavigationLink(destination: DetailView(item: item)) {
Card(item: item)
}
}
}
}
}
}
}
我的一个要求是,当您在列表中滑动时,背景图像会随着焦点项目而变化。
现在,我想做的是这个。 但是在NavigationLink
上覆盖.focusable()
似乎禁用了它的点击动作。
struct ContentView: View {
// Store Focused Item
@State private var currentItem: Item?
var body: some View {
NavigationView {
ScrollView(.horizontal) {
HStack {
ForEach(items) { item in
NavigationLink(destination: DetailView(item: item)) {
Card(item: item)
}
// Set which item is highlighted
.focusable(true) { isFocused in
currentItem = item
}
}
}
}
}
}
}
到目前为止我尝试过的:
我想我可以聪明一点,使用动态 NavigationLink,并制作一个可聚焦的按钮,为我设置一个标签。 这有相同的结果:(
struct ContentView: View {
@State private var currentItem: Item?
// Store Selection Tag
@State private var selection: String? = nil
var body: some View {
// ...
NavigationLink(destination: DetailView(item: item), tag: item.title, selection: $selection) {
EmptyView()
}
Button(action: { selection = item.title }) {
Card(item: item)
}
.buttonStyle(PlainButtonStyle())
.focusable(true) { isFocused in
currentItem = item
}
// ...
}
}
** 问题 **
.focusable()
可以配置在它仍然调用动作的地方吗?感谢您对此的任何帮助。 这看起来很简单,但我已经快用完了。
我找到了答案。 那是因为在引入 Focusable 时,它将打破当前的焦点引擎状态。 为了更好地控制焦点行为,不要使用 focusable,请使用 .onchange 作为焦点更改触发事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.