gpt4 book ai didi

ios - 自动布局不传递给子层

转载 作者:可可西里 更新时间:2023-11-01 06:20:36 27 4
gpt4 key购买 nike

我正在为 iOS v5.8.3(build 3)使用 Xamarin

我在另一个 UIView 上添加了一个带有 CAGradientLayerUIView 是这样的:

public static void AddGradientView (CGColor color1, CGColor color2, UIView view) {
UIView gradientView = new UIView (view.Bounds);
gradientView.BackgroundColor = UIColor.Green;
gradientView.AutoresizingMask = UIViewAutoresizing.All;

CAGradientLayer gradient = new CAGradientLayer ();
gradient.Frame = gradientView.Bounds;
gradient.Colors = new CGColor[]{ color1, color2 };

gradientView.Layer.InsertSublayer (gradient, 0);

view.AddSubview (gradientView);
}

但是结果是这样的:

enter image description here

问题是渐变层,他的 frame 不适合想要的 UIView。我认为这是一个 Autolayout 问题。

谁能给我一些启发?

提前致谢!

附言上面的代码是用 objective sharpie 写的,但是 iOS 开发者很容易理解 ;)

最佳答案

CALayer 及其子类不受自动布局的影响。

要更新图层框架,您可以使用 viewDidLayoutSubviews(如果您从 View Controller 更新 a),或者从自定义 UIView 子类更新 layoutSubviews

来自 UIViewController:

- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// you need to store a reference to the gradient layer and gradient views or fetch them from the sublayers and subviews
self.gradient.frame = self.gradientView.bounds;
}

UIView 子类:

- (void)layoutSubviews
{
[super layoutSubviews];
self.gradient.frame = self.bounds;
}

关于ios - 自动布局不传递给子层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29644384/

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