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
@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