现在的位置: 首页 > 综合 > 正文

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

2017年11月14日 ⁄ 综合 ⁄ 共 976字 ⁄ 字号 评论关闭

工程需要包含 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];
【上篇】
【下篇】

抱歉!评论已关闭.