How to disable the shadow of the navigation bar in iOS 6 for navigation bars with custom background images?

It seems that in iOS 6, the shadow is automatically added to the navigation bar even when setting up a custom background image. I am pretty sure that this does not apply to iOS 5, since when I test the same code in the simulator of iOS 5 and 6, the shadow appears in iOS 6, but not 5.

Does anyone know about this? Or how to enable / disable it?

+49
iphone ios6 shadow uinavigationbar
Jun 18 '12 at 13:50
source share
13 answers

Put it in your AppDelegate

[[UINavigationBar appearance] setShadowImage:[UIImage new]]; // is IOS 7 and later [[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; 

Here is what I did for me. Hope this helps!

Fast version with updates from comments

  UINavigationBar.appearance().shadowImage = UIImage() UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default) 
+140
Nov 09 '12 at 7:12
source share

I know this was resolved with the more complex answers above, but this is the quickest and easiest way to hide the shadow under the navigation bar.

 self.navigationController.navigationBar.clipsToBounds = YES; 
+56
Oct 14
source share

Note from Apple dev docs regarding shadowImage object:

Discussion: The default value is nil, which corresponds to the default shadow image. When non-nil, this property is a custom shadow image to display instead of the default value. For a custom shadow image, it is shown that a custom background image must also be set using 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, in order to use the nil UIImage hack, you must also customize the background image of the custom navigation bar. It can also be zero, which leads to a nice flat, clean subway style navigation bar:

 [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]]; [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; 
+25
Apr 25 '13 at 9:19
source share

You can also try the following:

 controller.navigationBar.shadowImage = [[[UIImage alloc] init] autorelease]; 
Controller

is a UINavigationController.

+19
Sep 18 '12 at 10:09
source share

Non-NDA Common Answer:

If you do not want something to stick out of the layer, mask the layer with its borders.

 [self.layer setMasksToBounds:YES]; 

Set the height explicitly to 44 (or 32 for landscape on iPhone) if that doesn't work on its own.

+7
Jul 08 '12 at 18:26
source share

Setting shadowImage to a null image works, however, the way the solution is presented leads to adding a property if the OS is earlier than iOS 6.

The best way to do something that depends on the existence of a property or method is:

 if ([self.navigationController.navigationBar respondsToSelector:@selector(shadowImage)]) { self.navigationController.navigationBar.shadowImage = [[[UIImage alloc] init] autorelease]; } 
+6
Oct 31 '12 at 17:27
source share

There are two possible solutions, the second of which is mentioned in other answers.

  • Add one transparent pixel to the bottom of your background image in the navigation bar, making it 45 feet high. This disables shadows in iOS 6.
  • Inject the following code:

     // Omit the conditional if minimum OS is iOS 6 or above if ([UINavigationBar instancesRespondToSelector:@selector(setShadowImage:)]) { [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]]; } 

Source: Advanced iOS Appearance Settings , @ 27:15

+3
Jan 26 '13 at 0:11
source share

Since self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init]; doesn't work, I found a simple and workable way to remove the UINavigationBar shadow in both iOS 6 and iOS 5. I hope that people who need can see this post.

All you have to do is prepare a single background image so that the height is 1 pixel larger than the height of your navigation bar (for example, 320 × 45 for the UINavigationBar by default, 640 × 90 for 2x, of course).

Then just use [[UINavigationBar appearance] setBackgroundImage: ...] , you will find that the shadow is replaced by 1 pixel. Hurrah!

By the way, I found that Twitter did the same if you unzip Twitter.ipa and look at bg_nav_bar_events_dark.png , size 320 × 47. They created their own 3 pixel shadow :)

+2
Sep 13 '13 at 7:39 on
source share

I can not comment, so I will add my information here.

Perhaps the above suggestions worked in beta, but it doesn't seem to be that way.

 self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init]; 

The above does not work, none of the other similar answers above. I tried them all.

Clipping to borders works, but does not give the result that I want, since I would like other views to hang outside the navigation bar.

+1
Oct 30 '12 at 21:16
source share

I came across this SO question while trying to make the navigation bars look the same between iOS6 and iOS7.

The answer I found was just used:

  NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]]; [titleBarAttributes setValue:[NSNumber numberWithInt:0] forKey:UITextAttributeTextShadowOffset]; [[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes]; 

i.e.: set the shadow offset to zero.

+1
Oct 16 '13 at 18:26
source share

I had the same problem and solved it by doing the following:

 CustomNavBar *navBar = (CustomNavBar *)self.navigationController.navigationBar; [navBar setBackgroundImage:[UIImage imageNamed:@"navigation_bar_gray.png"] forBarMetrics:UIBarMetricsDefault]; navBar.shadowImage = [[UIImage alloc]init]; // this is what acctually removed the shadow under navigation bar 
0
Oct 04 '12 at 7:18
source share

How about an alternative way:

UINavigationBar.appearance().barStyle = .Black

For dark navigation bars, iOS does not display a shadow.

0
Sep 29 '15 at 8:29
source share

In Swift 3.0, it will look like

 UINavigationBar.appearance().shadowImage = UIImage () UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default) 
0
Oct. 24 '16 at 8:50
source share



All Articles