Skip to content

Instantly share code, notes, and snippets.

@vinhnx
Last active October 31, 2024 06:34
Show Gist options
  • Save vinhnx/5b78bc13bfbc5d1070a5 to your computer and use it in GitHub Desktop.
Save vinhnx/5b78bc13bfbc5d1070a5 to your computer and use it in GitHub Desktop.
remove 1px bottom line of the navigation bar

If you just want to use a solid navigation bar color and have set this up in your storyboard, use this code in your AppDelegate class to remove the 1 pixel border via the appearance proxy:

Objective-C

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init]
                                  forBarPosition:UIBarPositionAny
                                      barMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

Swift 3.x

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

NOTE: must set isTransluscent UINavigationBar instance to false to avoid transparent navigation bar; either in storyboard or in code

navigationController?.navigationBar.isTranslucent = false

// or

navigationBar.isTranslucent = false
@anuragbhai
Copy link

I can hide the bottom line of Navigation Bar with this code:

 UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
        UINavigationBar.appearance().shadowImage = UIImage() 

But when i scroll on the screen, The navigation bar title overlaps with the content on screen?
Is this related with the tint colour?

@Cyanide7523
Copy link

Need help here. I used this code:

UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
        UINavigationBar.appearance().shadowImage = UIImage()

but when I run it, the whole navigation bar disappears. Help me..

@BilalReffas
Copy link

@Cyanide7523 should do it for youself.navigationBar.shadowImage = UIImage()

@hiteshinfostride
Copy link

The problem with this method is that. The 1px points disappears but flickers later.
To reproduce : Set navigation bar to a color and set the ViewController background as the same color.
Make app go to background, open from background. The 1px point line will flicker.

@pasovsky
Copy link

pasovsky commented Oct 5, 2020

Try navigationBar.setValue(true, forKey: "hidesShadow")

@AlexApriamashvili
Copy link

iOS 15:

Note: If you have any scrollable content (scroll view, table view) in your view controller beware of the new behaviour which comes with scrollEdgeAppearance introduced in iOS 15. This appearance by default produces a transparent background, to all navigation bars. The background is controlled by when your scroll view scrolls content behind the navigation bar.

To avoid having transparent nav bars on your screens and still be able to remove the bottom line, use:

func configureScrollEdgeAppearance() {
        if #available(iOS 15, *) {
            guard let navigationBar = navigationController?.navigationBar else { return }

            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground() // use `configureWithTransparentBackground` for transparent background
            appearance.backgroundColor = <your-background-color>
            appearance.shadowColor = .clear
            appearance.shadowImage = UIImage()

            navigationBar.standardAppearance = appearance
            navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment