【问题标题】:Setting gradient to StyledStringElement将渐变设置为 StyledStringElement
【发布时间】:2013-06-04 19:39:31
【问题描述】:

我正在尝试使用CAGradientLayerStyledStringElement 中的单元格设置渐变。这是我到目前为止的代码:

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


    【解决方案1】:

    各个单元格负责为自己渲染圆角。

    有几种方法,一种是在渲染期间进行(参见 ImageElement 示例)。另外,如果您选择沿着层路径向下,则在层上设置 CornerRadius 属性。

    【讨论】:

    • 谢谢,我会检查 ImageElement 并看看它是如何完成的。除此之外,有没有其他方法可以在不使用图像或使用图层的情况下做到这一点?我看到我只是在顶部添加了一个层,它根本不尊重按钮的边界,我觉得层路由可能不是这里的路。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多