SwiftUI: 从后台返回后触摸不起作用

huangapple go评论55阅读模式
英文:

SwiftUI: Touches not working after returning from background

问题

以下是翻译好的部分:

Got a strange bug/error. Touches stops working at the top after closing and open the app.

To reproduce:

  1. 点击蓝色条以触发 "onTapGesture"
  2. 向上滑动以返回到主屏幕
  3. 打开应用程序
  4. 向下拖动以关闭模态框
  5. 点击蓝色条(将不起作用)

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

这是否是SwiftUI中的一个bug?
是否有关于解决此问题的建议?

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")
    })
}

}

英文:

Got a strange bug/error. Touches stops working at the top after closing and open the app.

To reproduce:

  1. Click the blue bar to trigger "onTapGesture"
  2. Swipe up to go back to springboard
  3. Open the app
  4. Drag down to close the modal
  5. Click the blue bar (Will not work)

Interesting, if I remove the "Color.red.ignoresSafeArea()" It works as expected. In iOS 15, it also works as expected.

Is this a bug in SwiftUI?
Any suggestion for a workaround?

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")
        })
    }
}

答案1

得分: 1

I see the same happening on iPhone 14 Pro, iOS 16.2, Xcode 14.2

A workaround could be to dismiss the sheet when the app goes into the background:

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
            }
        }
    }
}
英文:

I see the same happening on iPhone 14 Pro, iOS 16.2, Xcode 14.2

A workaround could be to dismiss the sheet when the app goes into the background:

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

huangapple
  • 本文由 发表于 2023年2月8日 17:26:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定