具有渐变图层的表视图单元格 - >布局问题



我正在尝试为TableViewCell添加渐变。我正在更新布局子视图((中的渐变层。当我调试时,它看起来为图层框架设置了正确的值。但是渐变不是"重绘"或其他东西,因为在每次旋转时,渐变都会以先前方向的大小显示。我做错了什么?

public override void AwakeFromNib()
{
base.AwakeFromNib();
_gradient = new CAGradientLayer();
_gradient.Frame = ContentView.Bounds;
_gradient.BackgroundColor = UIColor.Red.CGColor;
_gradient.StartPoint = new CGPoint(0, 0);
_gradient.EndPoint = new CGPoint(1, 1);
_gradient.Colors = new[] { _topColor, _bottomColor };
this.ContentView.Layer.InsertSublayer(_gradient, 0);
}
public override void LayoutSubviews()
{
_gradient.Frame = ContentView.Bounds;
base.LayoutSubviews();
}

我按照@dip的建议将图层更新从布局子视图移动到绘图

public override void Draw(CGRect rect)
{
_gradient.Frame = ContentView.Bounds;
base.Draw(rect);
}

布局现在正在工作。

最新更新