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.

0

2 Answers 2

80

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

 navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    navigationController?.navigationBar.shadowImage = UIImage()
10
  • For swift3: UINavigationBar.appearance().shadowImage = UIImage() UINavigationBar.appearance().setBackgroundImage(UIImage(named: "img_name"),for:.default)
    – Lanston
    Nov 17, 2016 at 2: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? Dec 29, 2016 at 4:30
  • 1
    navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default) this line is causing my navigationBar to not respect barTint Dec 29, 2016 at 4:38
  • @TheRohanSanap if you're talking about barTintColor, then just set it after setBackgroundImage()
    – Xernox
    Jan 2, 2017 at 13:26
  • 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 - > stackoverflow.com/a/19227158/4618125 Jan 3, 2017 at 10:38
24

If you want to do it globally you can do:

UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
5
  • Works except when it segues into another view controller, my nav bar has no color.
    – Hedylove
    Jan 30, 2017 at 0:04
  • 1
    Where would you set this--- I am guessing in AppDel? Sample code?
    – UKDataGeek
    Mar 23, 2017 at 7:41
  • 2
    Update for Swift 4: UINavigationBar.appearance().shadowImage = UIImage() UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
    – user6338195
    Oct 31, 2017 at 9:24
  • 2
    @EdmundElmer ios 13 in not remove shadow Dec 21, 2019 at 12:38
  • 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, 2021 at 23:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.