【发布时间】:2013-06-04 19:39:31
【问题描述】:
我正在尝试使用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,所以当点击它时,它会在其周围显示正确的圆角矩形框。
此外,颜色看起来不错,但渐变看起来被拉伸了,因此按钮看起来像是渐变中的一种颜色,而不是渐变本身。
我做错了什么?
【问题讨论】:
标签: c# xamarin.ios monotouch.dialog