将标准按钮样式替换为自定义按钮样式后,该按钮在 tvOS 上不再可选(它在 iOS 上按预期工作)。 PlainButtonStyle() 中是否有我缺少的特殊修饰符?或者这是 SwiftUI 中的一个错误?
这是有效的截图:
Button(
action: { },
label: { Text("Start") }
).buttonStyle(PlainButtonStyle())
这是一个没有的:
Button(
action: { },
label: { Text("Start") }
).buttonStyle(RoundedButtonStyle())
其中 RoundedButtonStyle() 定义为:
struct RoundedButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding(6)
.foregroundColor(Color.white)
.background(Color.blue)
.cornerRadius(100)
}
}
如果您创建自己的风格,则必须手动处理焦点。当然,您可以通过不同的方式来做到这一点。
struct RoundedButtonStyle: ButtonStyle {
let focused: Bool
func makeBody(configuration: Configuration) -> some View {
configuration
.label
.padding(6)
.foregroundColor(Color.white)
.background(Color.blue)
.cornerRadius(100)
.shadow(color: .black, radius: self.focused ? 20 : 0, x: 0, y: 0) // 0
}
}
struct ContentView: View {
@State private var buttonFocus: Bool = false
var body: some View {
VStack {
Text("Hello World")
Button(
action: { },
label: { Text("Start") }
).buttonStyle(RoundedButtonStyle(focused: buttonFocus))
.focusable(true) { (value) in
self.buttonFocus = value
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)