1、创建一个UINavigationBar类别 .h文件的内容如下
@interface UINavigationBar (ShadowBar)
- (void)dropShadowWithOffset:(CGSize)offset
radius:(CGFloat)radius
color:(UIColor *)color
opacity:(float)opacity;
@end
.m文件的内容
#import "UINavigationBar+ShadowBar.h"
@implementation UINavigationBar (ShadowBar)
- (void)dropShadowWithOffset:(CGSize)offset
radius:(CGFloat)radius
color:(UIColor *)color
opacity:(float)opacity{
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds);
self.layer.shadowPath = path;
CGPathCloseSubpath(path);
CGPathRelease(path);
self.layer.shadowColor = color.CGColor;
self.layer.shadowOffset = offset;
self.layer.shadowRadius = radius;
self.layer.shadowOpacity = opacity;
self.clipsToBounds = NO;
}
@end
在viewcontroller中的使用
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.navigationController.navigationBar dropShadowWithOffset:CGSizeMake(1, 1) radius:5 color:[UIColor blackColor] opacity:0.8];