【问题标题】:SwiftUI Buttons (and everything else) with no style become unresponsive when other Button with custom style are present on screen on MacOS Catalyst当 MacOS Catalyst 上的屏幕上出现其他具有自定义样式的按钮时,没有样式的 SwiftUI 按钮(以及其他所有内容)变得无响应
【发布时间】:2021-03-24 03:26:07
【问题描述】:

我在 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
    }
}

没有样式的按钮(以及从选择器到滑块的所有其他内容)在多次渲染后变得无响应。 仅当两个或多个具有自定义样式的按钮在屏幕上可见时才会发生这种情况。 不同的风格并不能解决问题。 你以前遇到过这个问题吗?这是 Mac 上 SwiftUI 的错误吗?

【问题讨论】:

  • 我已经尝试了你的代码,在 mac 11.3-beta 上一切正常,使用 xcode 12.5-beta,目标 ios 14.x,mac catalyst 11.3。
  • 这很有趣。要复制该问题,您应该至少重复按一次自定义样式的所有三个按钮。你这样做了吗?感谢您尝试@workingdog
  • 是的,点击了很多次按钮。也尝试了不同的序列,一切仍然运行良好。
  • FWIW,我也检查了(因为我在 TextField 中遇到了类似的无响应问题),但这一切都对我有用,并且不会变得无响应。 XCode 12.4.

标签: ios swiftui mac-catalyst


【解决方案1】:

事实证明,在使用“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
    }
    
}

【讨论】:

  • 是的!我没有提到我正在为 Mac 和 iOS 构建一个应用程序,以便为我启用“为 Mac 优化”选项。
  • 似乎指定 contentShape 解决了这个问题,尽管我真的不明白在“为 Mac 优化界面”中使用 SwiftUI 的幕后发生了什么。将 Button 与 TextField 一起使用时,它给我带来了问题。提供上面的代码。
  • 希望有朝一日可以修复,我提交了 FB9101621
猜你喜欢
  • 1970-01-01
  • 2018-02-16
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多