Autolayout没有传递给子层

gra*_*n33 3 iphone calayer ios cagradientlayer autolayout

Xamarin用于iOSv5.8.3(版本3).

我在另一个谎言中添加了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);
}
Run Code Online (Sandbox Code Playgroud)

但结果是这样的:

在此输入图像描述

问题是梯度层,他frame不适合想要的UIView.我认为这是一个Autolayout问题.

任何人都可以对我说些什么吗?

提前致谢!

PS上面写的代码objective sharpie但是iOS开发人员可以很容易地理解;)

Nik*_*jic 11

CALayer及其子类不受autolayout的影响.

要更新图层框架,您可以使用viewDidLayoutSubviews它来从视图控制器或layoutSubviews自定义UIView子类更新.

从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;
}
Run Code Online (Sandbox Code Playgroud)

UIView子类:

- (void)layoutSubviews
{
     [super layoutSubviews];
     self.gradient.frame = self.bounds;
}
Run Code Online (Sandbox Code Playgroud)