21

Inner shadow example

I'm trying to put nav bar below the other one to make it look like one tall nav bar. But in iOS 7 UINavigationBar now has inner shadow on top and on bottom of it. I really need to remove it. But I didn't found any solution. It looks like the shadow is prerendered, but in fact it slowly appears in about 0.4 second after the view appears.

I've tried almost everything but the shadow is still there. I removed the horizontal line below the bar with this code:

for (UIView *view in [[[self.navigationController.navigationBar subviews] objectAtIndex:0] subviews]) {
     if ([view isKindOfClass:[UIImageView class]]) view.hidden = YES;
}

But I can't figure it out how to remove the shadow. Thanks a lot!

I've tried this:

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

But that code doesn't even remove the horizontal line below the bar (this method needs custom background image). I use Xcode Version 5.0 (5A11365x)

TOVVV
  • 211
  • 1
  • 2
  • 4
  • http://stackoverflow.com/tags/ios7/info, second paragraph. – CaptJak Aug 11 '13 at 16:43
  • @CaptJak your link doesn't provide any useful information (anymore?). I am interested to know more about why this is a bad idea. – Jarrod Sep 26 '13 at 13:47
  • @Jarrod, anymore is correct. I posted the comment in Aug, before iOS 7 was released. The second paragraph in the link stated that iOS 7 is under NDA. As it is no longer under NDA and there is also only one paragraph. The answer below concurs with the fact that answering this while under NDA is not a good idea. – CaptJak Sep 26 '13 at 17:19
  • @CaptJak ah yeah NDA. I took your comment to mean that removing the line was a bad idea. Thanks. – Jarrod Sep 26 '13 at 20:02
  • 1
    Thanks for the tip on how to remove line under nav bar! – Dannie P Oct 01 '13 at 10:51
  • Helpful: http://stackoverflow.com/questions/19226965/how-to-hide-ios7-uinavigationbar-1px-bottom-line – Niraj Mar 29 '16 at 07:32

6 Answers6

56

The "horizontal" line at the bottom of the navigation bar is simply it's shadowImage. It can simply be removed by applying an empty UIImage. According to the documentation you also have to set a custom background image:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set the background and shadow image to get rid of the line.
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}
Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126
Thyraz
  • 2,412
  • 2
  • 21
  • 23
21

I really shouldn't as CaptJak has pointed out but for anyone else who gets stuck:

for (UIView *view in self.navigationController.navigationBar.subviews) {
    for (UIView *view2 in view.subviews) {
        if ([view2 isKindOfClass:[UIImageView class]]) {
            [view2 removeFromSuperview];
        }
    }
}

enter image description here

Rambatino
  • 4,716
  • 1
  • 33
  • 56
  • Nope, in iOS7 that code removes just the bottom horizontal line. The shadow is still there. – TOVVV Aug 12 '13 at 08:38
  • There is no shadow on mine. I think you're referring to the shadow that is produced by coloured tableviewcells/tableviewheaders – Rambatino Aug 12 '13 at 09:33
  • It's not a tableview shadow. tableview is much lower that nav bar. Your code produces just the same result as mine on screenshot. Your code works only with this `code`self.navigationController.navigationBar.translucent = NO;`code` But that's not what I'm looking for – TOVVV Sep 15 '13 at 12:19
  • I finally get it. I have added new Nav Bar in my VC which is UNDER navigation bar of navigation controller. So, iOS 7 translucency gives me a shadow where my nav bar starts – TOVVV Sep 15 '13 at 19:11
  • 4
    It's a bit brute force though: this method also removes all UIBarButtonItems with UIImages in them :-/ I recommend checking the height `if([view2 isKindOfClass:[UIImageView class]] && view2.frame.size.height < 2)` – auco Oct 07 '13 at 15:49
  • Do you have the same hack for the tab bar ? – Michaël Dec 04 '13 at 16:43
  • I imagine it is the same prinicple. Try: for (UIView *view in self.tabBar.subview){NSLog(@"%@",view);for(UIView *view2 in view.subview){NSLog(@%@",view2);}} wrote that from memory, but should work. inspect the height of all the views and see which one you want to remove, it should be obvious – Rambatino Dec 05 '13 at 17:57
  • Why are people downvoting this answer? – Rambatino Dec 26 '13 at 16:47
9

In Swift (tested on iOS9)

self.navigationController!.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationController!.navigationBar.shadowImage = UIImage()
  • 1
    That worked nice but now I cannot set the navigationbar tint color, it keeps white. Any suggestion? Ok I got it, just add self.navigationController!.navigationBar.translucent = false before – Shyri Feb 16 '16 at 19:42
0

If your app support only iOS 6.0+, you can simply add below line:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
[[UINavigationBar appearance] setShadowImage:[UIImage new]];
...
}
ZYiOS
  • 5,204
  • 3
  • 39
  • 45
  • This will not work, as one will also need to set the background of the UINavigationBar, which makes it opaque. – wprater Dec 13 '13 at 21:53
  • Be careful with this suggestion. This will apply it app wide and not just for the controller at hand. – Etienne Sep 02 '16 at 12:06
0

I had a similar problem, where I wanted to remove the 1px line and the shadow from the navigation bar on iOS7. In my case, I needed a 3 points thick green line at the bottom of the navigation bar.

If there is a solution to get rid of the 1px line at the bottom of the navigation bar that doesn't involve traversing subviews, I don't know, but there is a way to add a view that hides that line as follows (at least the color of the line can be changed that way).

UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, navBarHeight, navBarWidth, 1)]; // Dont use magic numbers in your code
bgView.backgroundColor = //some other color
[navigationBar addSubview:bgView];
[navigationBar setShadowImage:nil];
Aurelien Cobb
  • 435
  • 4
  • 6
0

You can easily use this UInavigationBar category called UINavigationBar-Addition found here I have used the solution described in this answer but I couldn't get rid of the 1Px line under navigationBar

Community
  • 1
  • 1
Mohamed Saleh
  • 2,881
  • 1
  • 23
  • 35