outerstorm
outerstorm

Reputation: 742

SwiftUI Remove NavigationBar Bottom Border

When using SwiftUI how do you remove the bottom 1px border of a navigation bar?

iOS SwiftUI NavigationBar Example

Upvotes: 20

Views: 15876

Answers (6)

metal bar
metal bar

Reputation: 600

if you are calling UINavigationBarAppearance().configureWithDefaultBackground() do not forget to set appearance.shadowColor = .clear only after it:

extension UINavigationBar {
    static func changeAppearance(clear: Bool) {
        let appearance = UINavigationBarAppearance()

        // Before
        // appearance.shadowColor = .clear

        if clear {
            appearance.configureWithOpaqueBackground()
        } else {
            appearance.configureWithDefaultBackground()
        }

        // After
        appearance.shadowColor = .clear

        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().compactAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance
    }
}

struct MyView: View {
    init() {
        UINavigationBar.changeAppearance(clear: true)
    }
}

Upvotes: -1

Islom Alimov
Islom Alimov

Reputation: 472

I have also met this problem. Here is the almost similar post

But most of the answer had side effect. And for me, the best solution was this

UINavigationBar.appearance().barTintColor = .clear
UINavigationBar.appearance().backgroundColor = .clear 
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().shadowImage = UIImage()

and also want its important to set UIImage() not 'nil' to shadowImage and bacgroundImage. And I made navigation displayMode inline

.navigationBarTitle(Text(""), displayMode: .inline)

Upvotes: 9

Giannis Chatziveroglou
Giannis Chatziveroglou

Reputation: 206

This is what worked for me

UINavigationBar.appearance().shadowImage = UIImage()

Upvotes: 0

titusmagnus
titusmagnus

Reputation: 2082

In Xcode 12.4, this is the combination that worked for me:

init() {
    UINavigationBar.appearance().shadowImage = UIImage()
    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
    UINavigationBar.appearance().isTranslucent = false
    UINavigationBar.appearance().barTintColor = UIColor.red
}

Upvotes: -2

Jiří Zahálka
Jiří Zahálka

Reputation: 8268

SwiftUI, 1 line.

UINavigationBar.appearance().standardAppearance.shadowColor = .clear

I implemented that inside .onAppear {}

Upvotes: -2

Niels Hoogendoorn
Niels Hoogendoorn

Reputation: 738

In the initializer of your View you can set the appearance of your navigation bar. There you have to set the .shadowColor property to .clear.

init() {
    let appearance = UINavigationBarAppearance()
    appearance.shadowColor = .clear
    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
}

Upvotes: 46

Related Questions