Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide UINavigationBar 1px bottom line

I have an app that sometimes needs its navigation bar to blend in with the content.

Does anyone know how to get rid of or to change color of this annoying little bar?

On the image below situation i have - i'm talking about this 1px height line below "Root View Controller"

enter image description here

like image 669
Szymon Kuczur Avatar asked Oct 07 '13 14:10

Szymon Kuczur


People also ask

How do I hide the navigation bar line?

You can swipe up on the lines or hide them with Gesture hints. From Settings, tap Display, and then tap Navigation bar.

How do I hide navigation bar in storyboard?

Click on the controller that has the top bar navigate to the properties bar on the right hand side of Xcode. There is a drop down labeled Top Bar (as shown above) change this drop down to none.

How do I use the navigation bar in Xcode?

To add a navigation bar to your interface, the following steps are required: Set up Auto Layout rules to govern the position of the navigation bar in your interface. Create a root navigation item to supply the initial title. Configure a delegate object to handle user interactions with the navigation bar.


2 Answers

For iOS 13:

Use the .shadowColor property

If this property is nil or contains the clear color, the bar displays no shadow

For instance:

let navigationBar = navigationController?.navigationBar let navigationBarAppearance = UINavigationBarAppearance() navigationBarAppearance.shadowColor = .clear navigationBar?.scrollEdgeAppearance = navigationBarAppearance 

For iOS 12 and below:

To do this, you should set a custom shadow image. But for the shadow image to be shown you also need to set a custom background image, quote from Apple's documentation:

For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage(_:for:) method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.

So:

let navigationBar = navigationController!.navigationBar navigationBar.setBackgroundImage(#imageLiteral(resourceName: "BarBackground"),                                                         for: .default) navigationBar.shadowImage = UIImage() 

Above is the only "official" way to hide it. Unfortunately, it removes bar's translucency.

I don't want background image, just color##

You have those options:

  1. Solid color, no translucency:

     navigationBar.barTintColor = UIColor.redColor()  navigationBar.isTranslucent = false  navigationBar.setBackgroundImage(UIImage(), for: .default)  navigationBar.shadowImage = UIImage() 
  2. Create small background image filled with color and use it.

  3. Use 'hacky' method described below. It will also keep bar translucent.

How to keep bar translucent?##

To keep translucency you need another approach, it looks like a hack but works well. The shadow we're trying to remove is a hairline UIImageView somewhere under UINavigationBar. We can find it and hide/show it when needed.

Instructions below assume you need hairline hidden only in one controller of your UINavigationController hierarchy.

  1. Declare instance variable:

    private var shadowImageView: UIImageView? 
  2. Add method which finds this shadow (hairline) UIImageView:

    private func findShadowImage(under view: UIView) -> UIImageView? {     if view is UIImageView && view.bounds.size.height <= 1 {         return (view as! UIImageView)     }      for subview in view.subviews {         if let imageView = findShadowImage(under: subview) {             return imageView         }     }     return nil } 
  3. Add/edit viewWillAppear/viewWillDisappear methods:

    override func viewWillAppear(_ animated: Bool) {     super.viewWillAppear(animated)      if shadowImageView == nil {         shadowImageView = findShadowImage(under: navigationController!.navigationBar)     }     shadowImageView?.isHidden = true }  override func viewWillDisappear(_ animated: Bool) {     super.viewWillDisappear(animated)      shadowImageView?.isHidden = false } 

The same method should also work for UISearchBar hairline, and (almost) anything else you need to hide :)

Many thanks to @Leo Natan for the original idea!

like image 87
Serhii Yakovenko Avatar answered Sep 30 '22 22:09

Serhii Yakovenko


Here is the hack. Since it works on key paths might break in the future. But for now it works as expected.

Swift:

self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow") 

Objective C:

[self.navigationController.navigationBar setValue:@(YES) forKeyPath:@"hidesShadow"]; 
like image 37
Vishnuvardhan Avatar answered Sep 30 '22 23:09

Vishnuvardhan