【问题标题】:SwiftUI: Touches not working after returning from backgroundSwiftUI:从后台返回后触摸不起作用
【发布时间】:2023-02-08 18:33:12
【问题描述】:

有一个奇怪的错误/错误。关闭并打开应用程序后,触摸停止在顶部工作。

重现:

  1. 点击蓝条触发“onTapGesture”
  2. 向上滑动返回跳板
  3. 打开应用程序
  4. 向下拖动关闭模式
  5. 单击蓝色条(不起作用)

    有趣的是,如果我删除“Color.red.ignoresSafeArea()”,它会按预期工作。在 iOS 15 中,它也按预期工作。

    这是 SwiftUI 中的错误吗? 有什么解决方法的建议吗?

    public struct TestView: View {
        @State private var showModal = false
    
        public var body: some View {
            ZStack {
                Color.red.ignoresSafeArea()
                
                VStack(spacing: 0) {
                    Color.blue
                        .frame(height: 20)
                        .onTapGesture {
                            showModal = true
                        }
                    Color.white
                }
            }
            .sheet(isPresented: $showModal, content: {
                Text("HELLO")
            })
        }
    }
    

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    我在 iPhone 14 Pro、iOS 16.2、Xcode 14.2 上看到同样的情况

    解决方法是在应用程序进入后台时关闭工作表:

    struct TestView: View {
        
        @State private var showModal = false
        @Environment(.scenePhase) var scenePhase
    
        public var body: some View {
            ZStack {
                Color.red.ignoresSafeArea()
                
                VStack(spacing: 0) {
                    Color.blue
                        .frame(height: 20)
                        .onTapGesture {
                            showModal = true
                        }
                    Color.white
                }
            }
            .sheet(isPresented: $showModal, content: {
                Text("HELLO")
            })
            .onChange(of: scenePhase) { scenePhase in
                if scenePhase == .background {
                    showModal = false
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多