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

前端 未结 13 1364
后悔当初
后悔当初 2020-12-12 17:10

It seems in iOS 6, a drop shadow is automatically added to the navigation bar even when you set a custom background image. I\'m pretty sure this wasn\'t the case with iOS 5

相关标签:
13条回答
  • 2020-12-12 17:45

    Since self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init]; not working, I've found an easy and workable way to remove the shadow of UINavigationBar in both iOS 6 AND iOS 5. Hope people who need can see this post.

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

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

    BTW I found Twitter has done the exactly same thing, if you unzip Twitter.ipa and look into bg_nav_bar_events_dark.png, the size is 320×47. They made their own shadow for 3 pixels :)

    0 讨论(0)
  • 2020-12-12 17:47

    General, non-NDA-infringing answer:

    If you don't want something sticking out of a layer, mask the layer to its bounds.

    [self.layer setMasksToBounds:YES];
    

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

    0 讨论(0)
  • 2020-12-12 17:48

    I had the same problem and I've solved it by 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 讨论(0)
  • 2020-12-12 17:53

    Also you can try this:

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

    controller is a UINavigationController.

    0 讨论(0)
  • 2020-12-12 17:56

    How about the alternative way:

    UINavigationBar.appearance().barStyle = .Black

    For the dark navigation bars iOS doesn't show the shadow.

    0 讨论(0)
  • 2020-12-12 17:57

    I know this has been solved with more complicated answers above, but this is the quickest and easiest way I hid the shadow under the navigation bar.

    self.navigationController.navigationBar.clipsToBounds = YES;
    
    0 讨论(0)
提交回复
热议问题