[英]SwiftUI Buttons (and everything else) with no style become unresponsive when other Button with custom style are present on screen on MacOS Catalyst
我在 Mac Catalyst 上遇到了 SwiftUI 的这个问题,其中一个简单的视图如下所示:
struct ContentView: View {
@State var count : Int = 0
var body: some View {
HStack{
Button("tap me"){
count += 1
}
Button("test \(count)"){
count += 1
}.buttonStyle(CustomButtonStyle())
Button("test \(count)"){
count += 1
}.buttonStyle(CustomButtonStyle())
Button("test \(count)"){
count += 1
}.buttonStyle(CustomButtonStyle())
}
}
}
struct CustomButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration
.label
}
}
没有样式的按钮(以及从选择器到滑块的所有其他内容)在多次渲染后变得无响应。 仅当两个或多个具有自定义样式的按钮在屏幕上可见时才会发生这种情况。 拥有不同的 styles 并不能解决问题。 你以前遇到过这个问题吗? 是 Mac 上 SwiftUI 的错误吗?
事实证明,在使用“Optimize Interface for Mac”进行部署时,这个问题是可以重现的。 如果您使用“Scale Interface to Match iPad”,它可以毫无问题地工作。 出于某种原因,指定 contentShape 可以解决问题。 FWIW,仅指定非自定义的 contentShape(即,使用“Optimize Interface for Mac”时为本地)在 VStack 中工作,但在 HStack 中不起作用。
struct ContentView: View {
@State private var count: Int = 0
var body: some View {
HStack {
Button("tap me") { count += 1 }
.contentShape(Rectangle())
Button("test \(count)") { count += 1 }
.contentShape(Rectangle())
.buttonStyle(CustomButtonStyle())
Button("test \(count)") { count += 1 }
.contentShape(Rectangle())
.buttonStyle(CustomButtonStyle())
Button("test \(count)") { count += 1 }
.contentShape(Rectangle())
.buttonStyle(CustomButtonStyle())
}
}
}
struct CustomButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration
.label
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.