UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Navigation bug when dismissing view while focussing on empty .searchable() modifier

Forums > SwiftUI

When trying to navigate back from a view using the environment Dismiss value while also focussing on an empty searchable modifier the view you navigated back to becomes unresponsive. This is due to an empty UIView blocking any interaction with the view as seen in this screenshot:

Empty UIView blocking view after navigating back

This only occurs when the searchbar is focussed and empty when trying to navigate back. When there's a value in the searchbar everything works:

GIF of the bug

Am I doing something wrong here?

Tested on Xcode 14.2 iPhone 14 Pro (iOS 16.0) simulator.

import SwiftUI

struct MainPage: View {
    var body: some View {
        if #available(iOS 16.0, *) {
            NavigationStack {
                Text("Main view")
                NavigationLink(destination: DetailView()) {
                    Text("Click me")
                }
            }
        }
    }
}

struct DetailView: View {
    @Environment(\.dismiss) private var dismiss
    @State private var searchText = ""

    var body: some View {
        VStack {
            Text("Detail view")
            Button("Go back") {
                dismiss()
            }
        }
        .searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
    }
}

This bug only seems to happen when using NavigationStack or NavigationView with a .navigationViewStyle(.stack). When using NavigationView without a navigationViewStyle it seems to work fine. Currently I can work around this using the latter but I would prefer to use NavigationStack as NavigationView has become deprecated since iOS 16.0.

Any help is appreciated.

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.