[英]Why am I unable to position my view with a gradient sublayer with Auto Layout? It doesn't show up
I have the following code to create a view with a gradient background (using a sub layer) and then add it as a subview: 我有以下代码来创建带有渐变背景的视图(使用子图层),然后将其添加为子视图:
// Add a gradient overlay to make caption more legible (add a little extra to the sides to make it continue over the spacing between view controllers)
UIView *gradientView = [[UIView alloc] init];
gradientView.translatesAutoresizingMaskIntoConstraints = NO;
gradientView.userInteractionEnabled = NO;
// Add the gradient effect
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = gradientView.bounds;
gradient.colors = @[(id)[UIColor colorWithWhite:0.0 alpha:0.0].CGColor,
(id)[UIColor colorWithWhite:0.0 alpha:0.65].CGColor,
(id)[UIColor colorWithWhite:0.0 alpha:0.8].CGColor];
gradient.locations = @[@0.0, @0.77, @1.0];
[gradientView.layer insertSublayer:gradient atIndex:0];
[self.view addSubview:gradientView];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:gradientView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:gradientView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:gradientView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:gradientView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
However, when I run it, nothing shows up. 但是,当我运行它时,没有任何显示。 If I set it up with a frame instead of Auto Layout, it does work. 如果我使用框架而不是自动版面设置,它确实可以工作。 Why am I unable to do this with Auto Layout? 为什么我不能使用“自动布局”来做到这一点?
CALayer
s don't follow Auto Layout, only UIKit views. CALayer
不遵循自动版式,仅遵循UIKit视图。 Your gradient layer is begin added, but it probably just has a zero size frame. 您的渐变层开始添加,但是它可能只有零尺寸的框架。 Best bet is subclass UIView
and use +[UIView layerClass]
to return a CAGradientLayer
, then it will automatically match the size of the view under Auto Layout, otherwise you'll need to position your layers manually using something like layoutSubviews
. 最好的选择是UIView
子类,并使用+[UIView layerClass]
返回一个CAGradientLayer
,然后它将自动匹配“自动布局”下视图的大小,否则,您需要使用诸如layoutSubviews
类的方法手动layoutSubviews
图层。
When configuring the height and width constraints, pass nil
for the toItem:
parameter and 0 for the 2nd attribute. 在配置高度和宽度约束时,将nil
传递给toItem:
参数,将0传递给第二个属性。 I noticed that you set the constants to 0, which means your gradient view will have no size. 我注意到您将常数设置为0,这意味着您的渐变视图将没有大小。
Also, the height and width constraints should be added to the gradientView
, not the main view. 同样,应该将height和width约束添加到gradientView
,而不是主视图中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.