37

Is there a way to delete the bottom border that iOS7 automatically displays under the navigationbar?

Gnamm
  • 633
  • 2
  • 8
  • 17

9 Answers9

48

That does not work on iOS7 with navigation translucent or not...

A paste from Apple documentation;

Description The shadow image to be used for the navigation bar. The default value is nil, which corresponds to the default shadow image. When non-nil, this property represents a custom shadow image to show instead of the default. For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.

So basically you need to implement that setBackgroundImage. Additional note, on iOS7 you won't use appearance anymore but you'll modify the Navigation bar in the viewController context where you are now.

That is:

    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];

In my case I put this in viewDidLoad (custom behavior can be added for each UIViewController in the UINavigationViewController).

wolffan
  • 1,084
  • 10
  • 15
46

If i understand you correctly try

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
muffe
  • 2,285
  • 1
  • 19
  • 23
12

based on muffed2k answer+ programming Thomas comment, this is what I'm using to show UINavigationBar without background image (ios5.1/6.0) and without bottom border(ios7.0) :

  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6)
    {
        [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
        [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    }else
    {
        [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    }
user1105951
  • 2,259
  • 2
  • 34
  • 55
6

If you're using Swift and you come across this question, try this in your main ViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    /// ...

    navigationController?.navigationBar.shadowImage = UIImage();
    navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)

    //...
}

Based on @wolffan's answer above

A Fader Darkly
  • 3,516
  • 1
  • 22
  • 28
4

For me follwing worked on iOS 7 to 9+ when translucent is set to false

UINavigationBar.appearance().transluscent = false
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics:.Default)
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
2

I know there is an accepted answer to this already but another way to do it is setting clipToBounds to true.

Here is the one line of code to do it in swift

self.navigationController?.navigationBar.clipsToBounds = true

Worked for me like a charm.

  • 1
    @user5209290 is correct. My status bar no longer blended with the navigation bar when clipping to the bounds of the navigation bar. – Daniel Zhang Sep 14 '15 at 21:58
1

for objective c

self.navigationController.navigationBar.clipsToBounds = YES;
Elangovan
  • 3,469
  • 4
  • 31
  • 38
Aftab
  • 11
  • 5
1

Works like a charm: Swift 3.x version

    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
Fabrizio Prosperi
  • 1,398
  • 4
  • 18
  • 32
0

If you are targeting iOS 7 and are not setting a background image then this will work:

        CGFloat navigationBarWidth = self.navigationController.navigationBar.frame.size.width;
        CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
        CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;

        UIGraphicsBeginImageContextWithOptions(CGSizeMake(navigationBarWidth, navigationBarHeight + statusBarHeight), NO, 0.0);
        UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];

        //the following line takes away the border but only works if a background image is set (above)
        [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

I got the idea from @muffe2k's answer and this SO post.

Community
  • 1
  • 1
kraftydevil
  • 5,144
  • 6
  • 43
  • 65