导航栏渐变完美适配ipone - x 齐刘海
- (void)colorGradientStartColor:(UIColor *)startPoint endColor:(UIColor *)endColor
{
NSArray * subs =[self.navigationBar subviews];
UIView * bgview=nil;
for (UIView * v in subs)
{
NSString * classname = NSStringFromClass([v class]);
if ([classname isEqualToString:@"_UIBarBackground"] || [classname isEqualToString:@"UIBarBackground"])
{
bgview=v;
break;
}
}
//初始化CAGradientlayer对象,使它的大小为UIView的大小
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
CGRect rect = bgview.frame;
rect.size.height = [UIScreen statusAndNavigationBarHeight];
bgview.frame = rect;
gradientLayer.frame = bgview.frame;
//将CAGradientlayer对象添加在我们要设置背景色的视图的layer层
// [self.navigationController.navigationBar.layer addSublayer:gradientLayer];
[bgview.layer addSublayer:gradientLayer];
//设置渐变区域的起始和终止位置(范围为0-1)
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1, 0);
//设置颜色数组
gradientLayer.colors = @[(__bridge id)startPoint.CGColor,
(__bridge id)endColor.CGColor];
//设置颜色分割点(范围:0-1)
gradientLayer.locations = @[@(0.1f), @(1.0f)];
//隐藏黑线
[self.navigationBar setShadowImage:[UIImage new]];
}