我在 Mac Catalyst 上使用 SwiftUI 时遇到了这个问题,其中一个简单的 View 如下所示:
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
}
}
没有样式的按钮(以及从选择器到 slider 的所有其他内容)在多次渲染后变得无响应。 仅当屏幕上可见两个或多个具有自定义样式的 Button 时,才会发生这种情况。 拥有不同的风格并不能解决问题。 您以前遇到过这个问题吗?这是 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
}
}
关于ios - 当 MacOS Catalyst 屏幕上出现其他具有自定义样式的按钮时,没有样式的 SwiftUI 按钮(以及其他所有按钮)变得无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66774329/