iOS实践很常

iOS 实现渐变色的导航栏

2021-05-07  本文已影响0人  骑马纵天下
渐变色

渐变色使用的CAGradientLayer绘制,主要属性有开始渐变的位置和结束渐变的位置,用来控制颜色渐变的方向.方向是由起始坐标(startPoint)指向结束坐标(endPoint)

 // gradient
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    gradientLayer.startPoint = CGPointMake(0.1, 0.1);
    gradientLayer.endPoint = CGPointMake(1, 0.5);
    gradientLayer.colors = @[(__bridge id)[UIColor hexColorFromString:@"#00ADDE"].CGColor,
                             (__bridge id)[UIColor hexColorFromString:@"#00B8D6"].CGColor,
                             (__bridge id)[UIColor hexColorFromString:@"#00CAC9"].CGColor,
                             (__bridge id)[UIColor hexColorFromString:@"#00D6C1"].CGColor,
                             (__bridge id)[UIColor hexColorFromString:@"#00E0B8"].CGColor
    ];
    gradientLayer.locations = @[@(0), @(0.24f), @(0.48f), @(0.6f), @(0.85f)];
    self.layer.shadowColor = [UIColor colorWithRed:70/255.0 green:199/255.0 blue:200/255.0 alpha:0.5].CGColor;
    self.layer.shadowOffset = CGSizeMake(0,3);
    self.layer.shadowOpacity = 1;
    self.layer.shadowRadius = 9;
    [self.layer insertSublayer:gradientLayer atIndex:0];
起始和结束坐标说明
 gradientLayer.startPoint = CGPointMake(0.1, 0.1);
 gradientLayer.endPoint = CGPointMake(1, 0.5);
 gradientLayer.colors = @[(__bridge id)[UIColor hexColorFromString:@"#00ADDE"].CGColor,
                             (__bridge id)[UIColor hexColorFromString:@"#00B8D6"].CGColor,
                             (__bridge id)[UIColor hexColorFromString:@"#00CAC9"].CGColor,
                             (__bridge id)[UIColor hexColorFromString:@"#00D6C1"].CGColor,
                             (__bridge id)[UIColor hexColorFromString:@"#00E0B8"].CGColor
    ];
gradientLayer.locations = @[@(0), @(0.24f), @(0.48f), @(0.6f), @(0.85f)];
设置渐变色导航栏

渐变色视图画好后,我们只需要直接添加到导航控制器的navigationBar上即可。或者直接新建一个继承自UINavigationController的类,直接在上面添加如下。

/// 直接在self添加渐变视图会遮盖子视图 所有转换为图片 添加为背景图
[self.navigationBar setBackgroundImage:[UIImage convertViewToImage:[[HPNavigationBar alloc]initWithFrame:CGRectMake(0, -STATUS_BAR_HEIGHT, MN_WIDTH, NAVIGATION_HRIGHT)]] forBarMetrics:UIBarMetricsDefault];
+ (UIImage *)convertViewToImage:(UIView *)view{
    CGSize size = view.bounds.size;
    /**
     * size: 表示区域大小
     * opaque: 是否透明, NO - 半透明, YES - 非透明
     * scale: 屏幕密度(几倍像素)
     */
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
UIBarButtonItem *bedNumItem = [[UIBarButtonItem alloc]initWithTitle:@"001床" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
bedNumItem.tintColor = [UIColor whiteColor];
NSMutableDictionary *attributs = [NSMutableDictionary dictionary];
attributs[NSForegroundColorAttributeName] = [UIColor whiteColor];
attributs[NSFontAttributeName] = MNFontSize(14);
[bedNumItem setTitleTextAttributes:attributs forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = bedNumItem;
上一篇下一篇

猜你喜欢

热点阅读