Would like to know create drop shadow for UINavigationbar. I tried to create custom navigation bar background with drop shadow, but the drop shadow cover the background view.
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [[UIImage imageNamed:@"titleBar.png"] retain];;
[image drawInRect:rect];
[image release];
}
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(320,50);
return newSize;
}
@end
I also tried on following solution: http://www.travisboudreaux.com/adding-a-drop-shadow-to-a-uinavigationbar:
@interface UINavigationBar (dropshadow)
-(void) applyDefaultStyle;
@end
@implementation UINavigationBar (dropshadow)
-(void)willMoveToWindow:(UIWindow *)newWindow{
[self applyDefaultStyle];
}
- (void)applyDefaultStyle {
// add the drop shadow
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowOffset = CGSizeMake(0.0, 3.0);
self.layer.shadowOpacity = 0.25;
}
@end
It shows drop shadow for my navigationbar button, but no the navigation bar itself.
Final Solution:
Here's how I create drop shadow for UINavigationBar. Big thanks for MusiGenesis for pointing out the missing link of my code:
#import <QuartzCore/QuartzCore.h>
@interface UINavigationBar (CustomImage)
-(void) applyDefaultStyle;
@end
//Override For Custom Navigation Bar
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"titleBar.png"];
[image drawInRect:CGRectMake(0, 0, 320, 44)];
}
-(void)willMoveToWindow:(UIWindow *)newWindow{
[super willMoveToWindow:newWindow];
[self applyDefaultStyle];
}
- (void)applyDefaultStyle {
// add the drop shadow
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowOffset = CGSizeMake(0.0, 3);
self.layer.shadowOpacity = 0.25;
self.layer.masksToBounds = NO;
self.layer.shouldRasterize = YES;
}
@end
** Remember to import quartzcore or it will throw error.
question from:
https://stackoverflow.com/questions/5940076/how-to-create-uinavigationbar-drop-shadow 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…