添加到主视图的子视图的尾随约束不粘
trailing constraint for subview added to main view is not sticking
我有一个视图,其父视图的前导和尾随约束常量 = 20。我正在尝试向该视图添加一个渐变层,但它只绘制了 560 的宽度,这是情节提要中的宽度(大小 类 活动)
似乎尾部约束没有坚持,因为视图框架大小为 768,而 iPad 应该如此。我也没有遇到自动布局错误,当我将背景颜色设置为非透明颜色时,它看起来不错,但渐变仍然没有完全绘制。
有什么想法吗??
func setupGradient(view: UIView) {
let colorArray = [UIColor.blackColor().CGColor, UIColor.whiteColor().CGColor]
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = view.bounds
print(gradient.frame)
print(self.view.bounds)
gradient.startPoint = CGPoint(x: 0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
gradient.colors = colorArray
view.layer.addSublayer(gradient)
}
来自打印语句:
(0.0, 0.0, 560.0, 70.0)
(0.0, 0.0, 768.0, 1024.0)
自动布局和自动调整大小蒙版适用于 UIViews
,不适用于 CALayers
。
设置图层框架后,自动布局可以更改视图框架,但不能更改图层框架。
我们需要手动更改图层框架
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
gradientLayer.frame = gradientView.bounds;
}
我有一个视图,其父视图的前导和尾随约束常量 = 20。我正在尝试向该视图添加一个渐变层,但它只绘制了 560 的宽度,这是情节提要中的宽度(大小 类 活动)
似乎尾部约束没有坚持,因为视图框架大小为 768,而 iPad 应该如此。我也没有遇到自动布局错误,当我将背景颜色设置为非透明颜色时,它看起来不错,但渐变仍然没有完全绘制。
有什么想法吗??
func setupGradient(view: UIView) {
let colorArray = [UIColor.blackColor().CGColor, UIColor.whiteColor().CGColor]
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = view.bounds
print(gradient.frame)
print(self.view.bounds)
gradient.startPoint = CGPoint(x: 0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
gradient.colors = colorArray
view.layer.addSublayer(gradient)
}
来自打印语句: (0.0, 0.0, 560.0, 70.0) (0.0, 0.0, 768.0, 1024.0)
自动布局和自动调整大小蒙版适用于 UIViews
,不适用于 CALayers
。
设置图层框架后,自动布局可以更改视图框架,但不能更改图层框架。
我们需要手动更改图层框架
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
gradientLayer.frame = gradientView.bounds;
}