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

前端 未结 13 1362
后悔当初
后悔当初 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:32

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

    1. Add a single, transparent, pixel at the bottom of your navigation bar background image, making it 45pt tall. This disables the shadows in iOS 6.
    2. Implement 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 Appearance Customization on iOS, @27:15

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

    Note from the Apple dev docs on the subject of the shadowImage property:

    Discussion: 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 to use the nil UIImage hack you must also be setting a custom nav bar background image. This can be a nil image too, which results in a nice flat, clean 'metro' style nav bar :

    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
            [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    
    0 讨论(0)
  • 2020-12-12 17:40

    Place this in your AppDelegate

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

    This is what did it for me. Hope it helps!

    Swift version with updates from comments

        UINavigationBar.appearance().shadowImage = UIImage()
        UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
    
    0 讨论(0)
  • 2020-12-12 17:40

    Setting the shadowImage to a null image does work, however, the way the solution is presented results in adding a property if the OS is earlier than iOS 6.

    A better way to do something that is dependent on the existence of a property or method is:

    if ([self.navigationController.navigationBar
    respondsToSelector:@selector(shadowImage)]) {
    self.navigationController.navigationBar.shadowImage = [[[UIImage alloc] init] autorelease];
    }
    
    0 讨论(0)
  • 2020-12-12 17:41

    I came across this SO question when trying to get nav bars to look the same between iOS6 and iOS7.

    The answer I found worked was simply to use:

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

    ie: set the shadow offset to zero.

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

    In Swift 3.0 this would look like this

    UINavigationBar.appearance().shadowImage = UIImage ()
    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
    
    0 讨论(0)
提交回复
热议问题