工程需要包含 QuartzCore.Framework。
.h文件
#import <UIKit/UIKit.h> @interface UINavigationBar (TNDropShadow) - (void)dropShadowWithOffset:(CGSize)offset radius:(CGFloat)radius color:(UIColor *)color opacity:(CGFloat)opacity; @end
.m文件
#import "UINavigationBar+TNDropShadow.h" #import <QuartzCore/QuartzCore.h> @implementation UINavigationBar (TNDropShadow) - (void)dropShadowWithOffset:(CGSize)offset radius:(CGFloat)radius color:(UIColor *)color opacity:(CGFloat)opacity { // Creating shadow path for better performance 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; // Default clipsToBounds is YES, will clip off the shadow, so we disable it. self.clipsToBounds = NO; } @end
使用方法
[self.navigationController.navigationBar dropShadowWithOffset:CGSizeMake(0, 2) radius:2 color:[UIColor darkGrayColor] opacity:1];