iOS UIButton颜色渐变 titleLabel字体与图标不显示

//渐变颜色
        CAGradientLayer *_gradientLayer = [CAGradientLayer layer];
        _gradientLayer.colors = @[(__bridge id)[UIColor colorWithRed:255/255.0 green:157/255.0 blue:72/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:253/255.0 green:93/255.0 blue:59/255.0 alpha:1.0].CGColor];
        _gradientLayer.startPoint = CGPointMake(0.5, 0);
        _gradientLayer.endPoint = CGPointMake(0.5, 1);
        _gradientLayer.frame = addBtn.bounds;
        _gradientLayer.cornerRadius = 22;
        [addBtn.layer insertSublayer:_gradientLayer atIndex:0];//注意添加顺序 
 [addBtn setImage:[UIImage imageNamed:@"xxx"] forState:UIControlStateNormal];
    [addBtn bringSubviewToFront:addBtn.imageView];

这时title就会显示出来,但,image还是会被隐藏,我猜测应该是image的层级比较靠后一些吧,具体是不是没有验证,
我强行将imageview的优先级提到最前面,使用bringSubviewToFront。

你可能感兴趣的:(iOS UIButton颜色渐变 titleLabel字体与图标不显示)