gpt4 book ai didi

ios - 为什么我无法使用带有自动布局的渐变子层来定位我的 View ?它没有出现

转载 作者:行者123 更新时间:2023-11-29 03:12:58 27 4
gpt4 key购买 nike

我有以下代码来创建一个具有渐变背景的 View (使用子图层),然后将其添加为 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]];

但是,当我运行它时,什么也没有显示。如果我用框架而不是自动布局来设置它,它就可以工作。为什么我无法使用自动布局来执行此操作?

最佳答案

CALayer 不遵循自动布局,仅遵循 UIKit View 。您的渐变图层已开始添加,但它可能只有一个零尺寸的框架。最好的办法是子类 UIView 并使用 +[UIView layerClass] 返回一个 CAGradientLayer,然后它会自动匹配 Auto 下 View 的大小布局,否则您需要使用 layoutSubviews 之类的东西手动定位图层。

关于ios - 为什么我无法使用带有自动布局的渐变子层来定位我的 View ?它没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22002654/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com