设置导航栏渐变title

重写UIViewController方法 - (void)setTitle;

- (void)setTitle:(NSString *)title{

[super setTitle:title];

UIView *titleView=[[UIView alloc] init];

titleView.size = [title sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20]}];

self.navigationItem.titleView=titleView;


UILabel *titleL=[[UILabel alloc] init];

[titleView addSubview:titleL];

titleL.frame=titleView.bounds;

titleL.text=title;

titleL.textAlignment=NSTextAlignmentCenter;

titleL.font=[UIFont systemFontOfSize:20];


CAGradientLayer *gradientLayer=[CAGradientLayer layer];

gradientLayer.startPoint=CGPointMake(0, 0.2);

gradientLayer.endPoint=CGPointMake(0, 1);

gradientLayer.colors=@[(id)[UIColor redColor].CGColor, (id)[UIColor greenColor].CGColor];

[titleView.layer addSublayer:gradientLayer];

gradientLayer.frame=titleL.frame;

gradientLayer.mask=titleL.layer;

titleL.frame=gradientLayer.bounds;

}

你可能感兴趣的:(设置导航栏渐变title)