众所周知的原因,ios要想实现渐变的话,需要先知道渐变容器的尺寸和设置渐变layer的大小。
由于我们经常会使用到动态计算高度。这个时候就不好处理了。
今天,第一次体会到, layoutIfNeeded的威力。
当Masonry 布局完毕后,调用上面的layoutIfNeeded。则可以轻松得到布局控件的frame,从而为我们添加渐变提供先置条件。
解决问题。
-(void)layoutSubviews{
NSLog(@"%s",__func__);
return;
if (self.greatAgent) {
if (bgViewLayer) {
[bgViewLayer removeFromSuperlayer];
}
self.bgView.backgroundColor = [UIColor clearColor];
CAGradientLayer *gl = [CAGradientLayer layer];
gl.frame = CGRectMake(0, 0, mScreenWidth, mScreenHeight);// self.bgView.bounds;// self.agentTagsBGView.bounds;// CGRectMake(96,101,189,18);
gl.startPoint = CGPointMake(0, 0);
gl.endPoint = CGPointMake(0, 1);
gl.colors = @[(__bridge id)HexRGB(0xF9F0E4).CGColor,(__bridge id)HexRGB(0xEED9B8).CGColor];
gl.locations = @[@(0), @(1.0f)];
bgViewLayer = gl;
[self.bgView.layer addSublayer:bgViewLayer];
self.bgView.layer.cornerRadius = 8;
self.bgView.layer.masksToBounds = YES;
}else{
self.bgView.backgroundColor = [UIColor whiteColor];
}
}
此乃关键,有兴趣详细了解这个api吧。
[self layoutIfNeeded];
后续:还是有bug。不过可以先给一个大概的尺寸。
///////////////
书接上一会
目前使用延时
听说masonry布局不是立即生效,涉及到runloop?
所以最后使用了延时处理。结果OK。
网友评论