4

I can't remove the shadow from my UINavigationBar for some reason on iOS6. Why isn't this working? I've tried the following:

if ([[UINavigationBar appearance]respondsToSelector:@selector(setShadowImage:)]){
    [[UINavigationBar appearance]setShadowImage:[[UIImage alloc] init]];
}

if ([[UINavigationBar class]respondsToSelector:@selector(setShadowImage:)]){
    [[UINavigationBar appearance]setShadowImage:[[UIImage alloc] init]];
}
Undo
  • 25,519
  • 37
  • 106
  • 129
aroooo
  • 4,726
  • 8
  • 47
  • 81
  • Interesting. I would put a breakpoint inside that if to see if it's even called. – CodaFi Dec 29 '12 at 07:50
  • Neither get called, I'm modifying the navigation bar's background by checking if [UINavigationBar class] responds to appearance, and that seems to work just fine. I just can't get it working for setShadowImage. setShadowImage alone works fine. – aroooo Dec 29 '12 at 08:42
  • Exactly. If it doesn't respond, and the selector isn't even called, how would you ever expect your changes to be applied? – CodaFi Dec 29 '12 at 08:45
  • That's my question- I'm not sure why it isn't responding to the selector. I posted two different examples I've tried because I'm not sure if I'm just checking wrong object for a response. – aroooo Dec 29 '12 at 08:47
  • It's possible that the appearance proxy just isn't cutting it. Try a direct set. – CodaFi Dec 29 '12 at 08:51

3 Answers3

3

You have to do the work on a NavigationBar instance...

if ([navigationBarInstance respondsToSelector:@selector(setShadowImage:)]){
    [navigationBarInstance setShadowImage:[[UIImage alloc] init]];
}

Edit: If you for some reason really need to perform the check on the class. This will work:

if ([UINavigationBar instancesRespondToSelector:@selector(setShadowImage:)]) {
}
T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32
2

This had me stumped for a while until I read the docs!

NOTE: 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.

Mike Pollard
  • 10,195
  • 2
  • 37
  • 46
1

Mike Pollard has it right.

To remove the shadow underneath the UINavigationBar on iOS 6, you need to set a custom background image in addition to setting the shadow image to a blank UIImage.

CustomViewController.m

- (void)viewDidLoad
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Background"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

In the above example, "Background" would be a PNG image in your project.