Swiftui TVO:处理第一个按钮的焦点,以向下导航方向
我想关注底部hstack
hstack 的第一个按钮 当用户向下导航时。我该如何实现? 截至目前,该指南正在选择最近的元素。
代码:
import SwiftUI
struct DummyView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
contentView
parent
}
private var parent: some View {
VStack {
if #available(tvOS 15.0, *) {
HStack {
Spacer()
Button ("1") {}
Button ("2") {}
Button ("3") {}
Spacer()
}
.focusSection()
.border(Color.white, width: 2)
} else {
// Fallback on earlier versions
}
Spacer()
if #available(tvOS 15.0, *) {
HStack {
Button ("A") {}
Spacer()
Button ("B") {}
Spacer()
Button ("C") {}
}
.border(Color.white, width: 2)
.focusSection()
} else {
// Fallback on earlier versions
}
}
}
private var contentView: some View {
VStack {
Spacer()
Text("THIS IS DUMMY SCREEN")
Spacer()
}
}
}
ScreenShot:
I want to focus on the first Button A
of the bottom Hstack
when the user navigates downwards. How can I achieve that?
As of now, the guide is picking the nearest element.
Code:
import SwiftUI
struct DummyView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
contentView
parent
}
private var parent: some View {
VStack {
if #available(tvOS 15.0, *) {
HStack {
Spacer()
Button ("1") {}
Button ("2") {}
Button ("3") {}
Spacer()
}
.focusSection()
.border(Color.white, width: 2)
} else {
// Fallback on earlier versions
}
Spacer()
if #available(tvOS 15.0, *) {
HStack {
Button ("A") {}
Spacer()
Button ("B") {}
Spacer()
Button ("C") {}
}
.border(Color.white, width: 2)
.focusSection()
} else {
// Fallback on earlier versions
}
}
}
private var contentView: some View {
VStack {
Spacer()
Text("THIS IS DUMMY SCREEN")
Spacer()
}
}
}
Screenshot:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经使用@focusstate来实现这一目标。您可以将视图的焦点状态绑定到枚举,然后创建一个空视图,以截取/直接焦点在接收到焦点时:
I've used @FocusState to achieve this. You can bind the focus state of the view to an enum, then create an empty view to intercept/direct focus when it receives focus: