在UINavigationBar,NavigationBar,导航条的下方加上阴影

工程需要包含 QuartzCore.Framework。

.h文件

 1 #import <UIKit/UIKit.h>
 2 
 3 @interface UINavigationBar (TNDropShadow)
 4 
 5 - (void)dropShadowWithOffset:(CGSize)offset
 6                       radius:(CGFloat)radius
 7                        color:(UIColor *)color
 8                      opacity:(CGFloat)opacity;
 9 
10 @end

 


.m文件

 

 1 #import "UINavigationBar+TNDropShadow.h"
 2 #import <QuartzCore/QuartzCore.h>
 3 
 4 @implementation UINavigationBar (TNDropShadow)
 5 
 6 - (void)dropShadowWithOffset:(CGSize)offset
 7                       radius:(CGFloat)radius
 8                        color:(UIColor *)color
 9                      opacity:(CGFloat)opacity
10 {
11     // Creating shadow path for better performance
12     CGMutablePathRef path = CGPathCreateMutable();
13     CGPathAddRect(path, NULL, self.bounds);
14     self.layer.shadowPath = path;
15     CGPathCloseSubpath(path);
16     CGPathRelease(path);
17 
18     self.layer.shadowColor = color.CGColor;
19     self.layer.shadowOffset = offset;
20     self.layer.shadowRadius = radius;
21     self.layer.shadowOpacity = opacity;
22 
23     // Default clipsToBounds is YES, will clip off the shadow, so we disable it.
24     self.clipsToBounds = NO;
25 
26 }
27 
28 @end


使用方法

 

1 [self.navigationController.navigationBar dropShadowWithOffset:CGSizeMake(0, 2)
2                                                            radius:2
3                                                             color:[UIColor darkGrayColor]
4                                                           opacity:1];

 

posted @ 2014-03-02 21:17  无名盗闪  阅读(356)  评论(0编辑  收藏  举报