当用Masonry设置视图或控件布局时,是不能立即拿到视图或者控件的Frame,此时有两种方法可以拿到Frame。
1.调用父视图的 layoutIfNeeded 方法后再获取frame;
eg:
UIView *bgV = [[UIView alloc] init];
[self.view addSubView:bgV];
[bgV mas_makeConstraints:^(UPMASConstraintMaker *make) {
make.left.equalTo(self.curveView).offset(110.5);
make.top.equalTo(self.curveView).offset(21.5);
make.width.equalTo(@70);
make.bottom.equalTo(self.curveView).offset(-12);
}];
[self.view layoutIfNeeded];
NSLog(@"bgV.frame为:%@",bgV.frame);此时可以获取到frame进行相关操作(添加layer)
2.延时0.1S后获取Frame;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//设置渐变色
CAGradientLayer *gradient = [CAGradientLayer layer];
//设置开始和结束位置(设置渐变的方向)
gradient.startPoint = CGPointMake(0, 0);
gradient.endPoint = CGPointMake(0, 1);
gradient.frame = self.changeColorView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithHexString:@"FFFFFF"].CGColor,(id)[UIColor colorWithHexString:@"F8F8F8"].CGColor,nil];
[self.changeColorView.layer insertSublayer:gradient atIndex:0];
});
网友评论