Skip to content

Instantly share code, notes, and snippets.

@vinhnx
Last active June 14, 2022 02:32
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • 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
@Gerst20051
Copy link

👍 @vinhnx any code for swift?

@ryanio
Copy link

ryanio commented Mar 31, 2015

@Gerst20051 here it is in Swift:

navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarPosition: .Any, barMetrics: .Default)
navigationController?.navigationBar.shadowImage = UIImage()

@nadalalyafaie
Copy link

THANK YOU SIR @ryanio

@efrenjgb
Copy link

efrenjgb commented Dec 8, 2015

if you want it application wide using Swift just add in your AppDelegate:

UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: .Any, barMetrics: .Default)
UINavigationBar.appearance().shadowImage = UIImage()

@Horse888
Copy link

just for solid navigation bar color

@wsrzx
Copy link

wsrzx commented Jul 5, 2016

Xamarin C#

NavigationController.NavigationBar.SetBackgroundImage(new UIImage(), UIBarPosition.Any, UIBarMetrics.Default);

@iujlaki
Copy link

iujlaki commented Dec 28, 2016

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

@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