uibutton增加漸變色的時(shí)候一定要注意不能直接addsublayer旅薄,這樣會(huì)導(dǎo)致新增加的layer在最上面睬塌,會(huì)遮擋title和image
應(yīng)該使用insertSublayer將漸變色的layer增加在最下層
'''
[_adoptAnswerTagBtn.layer insertSublayer:self.gradientLayer atIndex:0];
'''
這時(shí)title就會(huì)顯示出來,但卸耘,image還是會(huì)被隱藏姜贡,我猜測(cè)應(yīng)該是image的層級(jí)比較靠后一些吧括儒,具體是不是沒有驗(yàn)證雇庙,
我強(qiáng)行將imageview的優(yōu)先級(jí)提到最前面鱼鼓,使用bringSubviewToFront。
具體代碼如下:
'''
- (UIButton *)adoptAnswerTagBtn
{
if(!_adoptAnswerTagBtn) {
_adoptAnswerTagBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_adoptAnswerTagBtn.titleLabel.font = [UIFont systemFontOfSize:14];
[_adoptAnswerTagBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_adoptAnswerTagBtn addTarget:self action:@selector(adoptAnswerBtnClick) forControlEvents:UIControlEventTouchUpInside];
_adoptAnswerTagBtn.layer.cornerRadius = 11.5;
_adoptAnswerTagBtn.layer.masksToBounds = YES;
_adoptAnswerTagBtn.size = CGSizeMake(64, 23);
self.gradientLayer.frame = _adoptAnswerTagBtn.bounds;
[_adoptAnswerTagBtn.layer insertSublayer:self.gradientLayer atIndex:0];
[_adoptAnswerTagBtn setImage:[UIImage imageNamed:@"ef_fundbar_answeradopt_white"] forState:UIControlStateNormal];
[_adoptAnswerTagBtn setTitle:@"采納" forState:UIControlStateNormal];
[_adoptAnswerTagBtn bringSubviewToFront:_adoptAnswerTagBtn.imageView];
_adoptAnswerTagBtn.hidden = YES;
}
return _adoptAnswerTagBtn;
} - (CAGradientLayer *)gradientLayer {
if (!_gradientLayer) {
_gradientLayer = [CAGradientLayer layer];
_gradientLayer.colors = @[(id)[RGB_HEX(0xE8CC94) CGColor],
(id)[RGB_HEX(0xD6AC6C) CGColor]
];
_gradientLayer.startPoint = CGPointMake(0, 0);
_gradientLayer.endPoint = CGPointMake(1.0, 1.0);
}
return _gradientLayer;
}
'''