BlackMouse
BlackMouse

Reputation: 4552

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

Upvotes: 2

Views: 429

Answers (1)

Ashley Mills
Ashley Mills

Reputation: 53181

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

Upvotes: 1

Related Questions