Home > Blockchain >  iOS 15 Navigation Bar
iOS 15 Navigation Bar

Time:02-24

I'm having this issue with the Navigation Bar in SwiftUI, where I don't want the Second View to have the ContentView's Navigation Bar. The preview in the SecondView behaves normal, but once I run the simulator and using the NavigationLink to go to SecondView, this is what it looks like.

import SwiftUI

struct ContentView: View {
    init() {
        let coloredAppearance = UINavigationBarAppearance()
        coloredAppearance.configureWithTransparentBackground()
        coloredAppearance.backgroundColor = UIColor(
            red: 41/255,
            green: 59/255,
            blue: 77/255,
            alpha: 1)
        coloredAppearance.shadowColor = .clear
        coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
        coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        
        UINavigationBar.appearance().standardAppearance = coloredAppearance
        UINavigationBar.appearance().compactAppearance = coloredAppearance
        UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
        UINavigationBar.appearance().tintColor = .white
    }
    var body: some View {
        NavigationView {
            NavigationLink(destination: SecondView()) {
                Text("Go to Second View")
                    .navigationTitle("SwiftUI")
            }
        }
    }
}

struct SecondView: View {
    var body: some View {
        NavigationView {
            Text("Second View")
                .navigationBarTitleDisplayMode(.inline)
                .ignoresSafeArea()
        }
        
    }
}

CodePudding user response:

struct SecondView: View {
    var body: some View {
        NavigationView {
            Text("Second View").navigationBarTitle("", displayMode: .inline)
                .navigationBarHidden(true)
        }.navigationBarHidden(true)
    }
}

I have tested this and it removes the navigation bar completely

  • Related