简体   繁体   English

为什么我无法使用“自动布局”使用渐变子图层来放置视图? 没有出现

[英]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.

相关问题 为什么我的 UITableView 单元格的渐变子层无法正常显示? - Why isn’t the gradient sublayer of my UITableView cell displaying properly? 为什么我的情节提要中的视图不显示(快速)? - why the view in my storyboard doesn't show up (Swift)? 为什么选项卡栏没有显示在我的视图控制器上? - Why doesn't the tab bar show up on my view controllers? 将约束添加到添加到UIWindow的视图时,为什么无法设置自动布局约束? - Why am I unable to set Auto Layout constraints when adding constraints to a view I added to UIWindow? 为什么我的渐变子图层显示异常? - Why is my gradient sublayer displaying weirdly? 我正在尝试将渐变添加为视图中的子层,但它没有将其大小调整为等于视图 - I am trying to add gradient as a sublayer in view but its not resizing it equal to the view 为什么我的导航栏不显示? - Why my navbar doesn't show up? addSublayer不会在视图上添加任何子图层 - addSublayer doesn't add any sublayer on the view 自动布局的新手-为什么我的视图不显示? - New to Auto Layout - why isn't my view appearing? 为什么第二次按下视图时后退按钮不显示? - Why doesn't the back button show up the second time I push the view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM