Question

Hey I am trying to add a shadow to all UINavigationBars in my project. I have tried a number of things recommended on this site and on other internet tutorials but have had no luck in getting a shadow to stick. I have tried putting it in using a shadow image I have created in photoshop, and calling the following in the main appDelegate:

[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:@"shadowTest.png"]];

I have subclassed the UINavigationBar out and tried to assign the property from within the class inside the awakeFromNib method and still nothing. I then tried to manually create the shadow with <QuartzCore/QuartzCore.h> image framework but nothing happened. I am using the subclass to override the drawRect method, could this be causing my problem? Here is the UINavigationBar class with my latest attempt at getting the shadow to stick.

#import "BasicNavigationBar.h"

@implementation BasicNavigationBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self)
    {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];

    // draw shadow
    self.layer.masksToBounds = NO;
    self.layer.shadowOffset = CGSizeMake(0, 3);
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, rect);
}

@end
Was it helpful?

Solution

setShadowImage doesn't work if you don't set the backgroundImage, too.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top