18

So I have a navigation bar and it has a horizontal line that I'd like to remove.

Screenshot

I have removed the nav bar color from the screenshot to make the line more apparent.

I have tried setting the shadow image property of the navigation bar to a blank png (1x1 pixel pngs for 1x, 2x and 3x), but there's no effect.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
TimSim
  • 3,936
  • 7
  • 46
  • 83

2 Answers2

80

Those two lines of code always do the trick for me :

 navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    navigationController?.navigationBar.shadowImage = UIImage()
Joseph Francis
  • 1,111
  • 1
  • 15
  • 26
FruitAddict
  • 2,042
  • 15
  • 16
  • For swift3: UINavigationBar.appearance().shadowImage = UIImage() UINavigationBar.appearance().setBackgroundImage(UIImage(named: "img_name"),for:.default) – Lanston Nov 17 '16 at 02:55
  • 1
    I have barTint and tintColor set for the navigationBar. After doing what you suggested, the barTint is not being respected and it is turning back to white. Any suggestions there? – Rohan Sanap Dec 29 '16 at 04:30
  • 1
    `navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)` this line is causing my navigationBar to not respect barTint – Rohan Sanap Dec 29 '16 at 04:38
  • @TheRohanSanap if you're talking about `barTintColor`, then just set it after `setBackgroundImage()` – Xernox Jan 02 '17 at 13:26
  • Can't edit, but you also have to set `.isTranslucent = false`. In my case I set it before setting `barTintColor` – Xernox Jan 03 '17 at 08:29
  • 1
    @Xernox No, that doesn't help. The method described in the above answer not only removes the bottom shadow but also makes the navigation bar clear. To remove the bottom shadow with retaining the barTintColor, follow this answer - > http://stackoverflow.com/a/19227158/4618125 – Rohan Sanap Jan 03 '17 at 10:38
  • @TheRohanSanap Are you referring to the original answer or to my suggestion? Because my suggestion does work and does not make navigation bar clear color. – Xernox Jan 03 '17 at 12:34
  • @Xernox your suggestion doesn't work with me. – Rohan Sanap Jan 03 '17 at 12:36
  • @Vishnuvardhan 's answer from https://stackoverflow.com/questions/19226965/how-to-hide-uinavigationbar-1px-bottom-line/19227158#19227158 does not remove barTintColor. – fs_tigre Nov 18 '18 at 12:44
  • This always worked for me except iOS 15 appears to require an image with content - I updated to use an image that's just a solid color. – gngrwzrd Jul 14 '21 at 23:18
24

If you want to do it globally you can do:

UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
Laszlo
  • 2,803
  • 2
  • 28
  • 33
Kubba
  • 3,390
  • 19
  • 34