如何创buildUINavigationBar阴影

想知道为UINavigationbar创build投影。 我试图用投影来创build自定义的导航栏背景,但是投影覆盖背景视图。

@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 

它显示我的导航栏button的投影,但没有导航栏本身。

最终解决scheme:以下是我为UINavigationBar创build投影的方法。 非常感谢MusiGenesis指出我的代码缺失的链接:

 #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 

**记得要inputquartzcore,否则会出错。

applyDefaultStyle ,尝试添加以下行:

 self.layer.masksToBounds = NO; 

这个属性的默认值是YES ,这意味着即使阴影被渲染,它也不会被渲染到视图的边界之外,这意味着你根本看不到它。

如果您以任何方式为此视图制作animation,则还应添加以下行:

 self.layer.shouldRasterize = YES; 

…除非你想要animation缓慢和生涩。

如果将投影应用于UINavigationBar ,则将阴影限制在angular点下方:

削减的阴影

这只是阴影在矩形上的performance。 我通常会为影子创build一个比实际导航栏更宽的path,这会产生更像您通常所期望的效果:

 @implementation UINavigationBar (DropShadow) -(void)willMoveToWindow:(UIWindow *)newWindow { [super willMoveToWindow:newWindow]; self.layer.shadowColor = [UIColor blackColor].CGColor; self.layer.shadowOpacity = 1; self.layer.shadowOffset = CGSizeMake(0,4); CGRect shadowPath = CGRectMake(self.layer.bounds.origin.x - 10, self.layer.bounds.size.height - 6, self.layer.bounds.size.width + 20, 5); self.layer.shadowPath = [UIBezierPath bezierPathWithRect:shadowPath].CGPath; self.layer.shouldRasterize = YES; } 

更好

从iOS 6.0开始,UINavigationBar具有一个属性shadowImage:

 @property(nonatomic,retain) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; 

这当然大大简化了这个非常普通的任务:D

我将阴影绘图代码粘贴到一个函数中,以保持loadView清洁。 如果你想要所有阴影一致,你可以传入要绘制阴影的对象。

 - (void)loadView { self.view = [[UIView alloc] init]; self.view.backgroundColor = [UIColor whiteColor]; [self drawShadow]; } - (void)drawShadow { self.navigationController.navigationBar.backgroundColor = [UIColor clearColor]; self.navigationController.navigationBar.layer.shadowOpacity = 0.3; self.navigationController.navigationBar.layer.shadowOffset = CGSizeMake(0, 0); self.navigationController.navigationBar.layer.shadowRadius = 15; self.navigationController.navigationBar.layer.masksToBounds = NO; }