英文:
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:
- 点击蓝色条以触发 "onTapGesture"
 - 向上滑动以返回到主屏幕
 - 打开应用程序
 - 向下拖动以关闭模态框
 - 点击蓝色条(将不起作用)
 
有趣的是,如果我移除 "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:
- Click the blue bar to trigger "onTapGesture"
 - Swipe up to go back to springboard
 - Open the app
 - Drag down to close the modal
 - 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
            }
        }
    }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论