如何创建UINavigationBar投影 [英] How to create UINavigationBar drop shadow

查看:88
本文介绍了如何创建UINavigationBar投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道为UINavigationbar创建投影。我尝试使用投影创建自定义导航栏背景,但投影覆盖背景视图。

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.

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

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

**记得导入quartzcore或者它会抛出错误。

** Remember to import quartzcore or it will throw error.

推荐答案

applyDefaultStyle 中,尝试添加此项line:

In applyDefaultStyle, try adding this line:

self.layer.masksToBounds = NO;

此属性的默认值为 YES ,这意味着即使渲染阴影,它也不会在视图范围之外渲染,这实际上意味着你根本看不到它。

The default value for this property is YES, which means that even though the shadow is rendered, it won't be rendered outside the bounds of the view, which means effectively that you don't see it at all.

如果您以任何方式设置此视图的动画,您还应该添加以下行:

If you're animating this view in any way, you should also add this line:

self.layer.shouldRasterize = YES;

...除非你想让动画变得缓慢而生涩。

... unless you want the animation to be slow and jerky.

这篇关于如何创建UINavigationBar投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆