我正在尝试CAGradientLayer
使用StyledStringElement
. 这是我到目前为止的代码:
public class DerreckStyledStringElement : StyledStringElement
{
public DerreckStyledStringElement(string caption) : base(caption) {
}
public override UITableViewCell GetCell(UITableView tv)
{
var x = base.GetCell(tv);
CAGradientLayer g = new CAGradientLayer();
g.Frame = x.Frame;
g.MasksToBounds = true;
x.ClipsToBounds = true;
g.Colors = new MonoTouch.CoreGraphics.CGColor[]
{
new UIColor(0f, 153f, 235f, 255f).CGColor,
new UIColor(0f, 197f, 244f, 255f).CGColor
};
UIView bg = new UIView(x.Bounds);
bg.TranslatesAutoresizingMaskIntoConstraints = true;
bg.Layer.InsertSublayer(g, 0);
x.BackgroundView = bg;
this.TextColor = UIColor.White;
return x;
}
}
我正在测试的按钮,它本身就坐,看起来像一个没有圆角的正方形,右侧延伸到设备的右边缘。最有可能的是,它可能会延伸到离屏幕更远的位置,因为按钮的左边距看起来是正确的。
What it should look like
| /---------------------\ |
| | BUTTON TEXT | |
| \---------------------/ |
What it looks like
| |------------------------|
| | BUTTON TEXT |
| |------------------------|
我使用g.MasksToBounds = true;
、x.ClipsToBounds = true;
和的地方bg.TranslatesAutoresizingMaskIntoConstraints = true;
是试图让背景剪辑到按钮框架本身。
因为我没有修改 SelectedBackground,所以当点击它时,它会在它周围显示正确的圆角矩形框。
此外,颜色看起来正确,但渐变看起来被拉伸,因此按钮看起来像是渐变中的一种颜色,而不是渐变本身。
我究竟做错了什么?