DEV Community

Kenji Wada
Kenji Wada

Posted on

Application crashes when using displayMode in SwiftUI's NavigationView

When you specify displayMode in NavigationView, the app crashes when it starts. This bug has been fixed in Xcode 12 beta 2. However, the crash will always occur on Xcode 11.6.

Environment

  • Xcode 11.6
  • iOS 13.6

I have a problem!!

This example code is correct.

struct HomeView : View {

    var body: some View {
        NavigationView {
            VStack {
                Text("ねこ")
            }
            .navigationViewStyle(StackNavigationViewStyle())
            .navigationBarTitle(R.string.localizable.home_title())
            .navigationBarItems(leading: Text("設定"))
        }
        .onAppear {
            FA.track(page: "ホーム")
        }
    }
}

This sample code crashes when the app is launched.

struct HomeView : View {

    var body: some View {
        NavigationView {
            VStack {
                Text("ねこ")
            }
            .navigationViewStyle(StackNavigationViewStyle())
            .navigationBarTitle(Text(R.string.localizable.home_title()), displayMode: .inline)
            .navigationBarItems(leading: Text("設定"))
        }
        .onAppear {
            FA.track(page: "ホーム")
        }
    }
}

I've solved the problem!

Apparently this has been fixed in Xcode 12 Beta2, but as of Xcode 11.6, the app will crash if the navigation bar transparency flag is set to false.

UINavigationBar.appearance().isTranslucent = false

If you set it to true, the app will not crash.

UINavigationBar.appearance().isTranslucent = true

Note

The original article is "SwiftUIで NavigationViewでdisplayModeを指定するとアプリ実行時にクラッシュする". I use machine translation.

Top comments (0)