iOS:如何获取视图';布局使用砌体的边界
我有个问题。iOS:如何获取视图';布局使用砌体的边界,ios,masonry,Ios,Masonry,我有个问题。 我使用状态栏。现在我想在状态栏中添加一个CAGradientLayer。所以我需要获取statusBar.bounds。但是边界是零。砌体不能为一层添加约束。非常感谢你! 这是我的代码: self.statusBar = [[UIView alloc] init]; [self addSubview:self.statusBar]; [_statusBar mas_makeConstraints:^(MASConstraintMaker *make) {
我使用状态栏。现在我想在状态栏中添加一个CAGradientLayer。所以我需要获取statusBar.bounds。但是边界是零。砌体不能为一层添加约束。非常感谢你! 这是我的代码:
self.statusBar = [[UIView alloc] init];
[self addSubview:self.statusBar];
[_statusBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(self);
make.height.equalTo(@(20));
make.top.left.equalTo(@(0));
}];
[self setStatusBarColorWithStartColor:[UIColor blackColor] endColor:[UIColor grayColor]];
- (void)setStatusBarColorWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor
{
// init CAGradientLayer
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
NSLog(@"%@", NSStringFromCGRect(self.statusBar.frame));
gradientLayer.frame = self.statusBar.bounds; // bound is zero
[self.statusBar.layer addSublayer:gradientLayer];
// set gradient colors
gradientLayer.colors = @[(__bridge id)startColor.CGColor, (__bridge id)endColor.CGColor];
// set star and end point
gradientLayer.startPoint = CGPointMake(0.0f, 0.5f);
gradientLayer.endPoint = CGPointMake(1.0f, 0.5f);
// optional set locations
}我想你需要这样的东西:
[self.statusBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(@(0));
make.top.equalTo(@(0));
make.right.equalTo(@(0));
make.height.equalTo(@(20));
}];
我想你需要这样的东西:
[self.statusBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(@(0));
make.top.equalTo(@(0));
make.right.equalTo(@(0));
make.height.equalTo(@(20));
}];
您可以通过覆盖视图的方法layoutSubviews或视图控制器的viewdilayoutsubviews获得正确的帧和边界。请小心,这两个选择器将被多次调用,viewDidLayoutSubviews需要调用[super viewDidLayoutSubviews]您可以通过覆盖视图的方法layoutSubviews或视图控制器的viewDidLayoutSubviews获得正确的帧和边界。请小心,这两个选择器将被多次调用,viewdlayoutsubviews需要调用[super-viewdlayoutsubviews]我尝试过,但边界仍然为零。也谢谢你,我试过了,但界限仍然是零。也谢谢你谢谢你。我从你的回答中得到了灵感。在此代码之前,
gradientLayer.frame=self.statusBar.bounds;//绑定为零
,我添加了[自布局需要]代码>。非常感谢@谢谢你。我从你的回答中得到了灵感。在此代码之前,gradientLayer.frame=self.statusBar.bounds;//绑定为零
,我添加了[自布局需要]代码>。非常感谢@Yahoho