如何在 Xamarin.iOS 的 UITableView 上仅圆角两个角?

5

我正在使用当前版本的Xamarin.iOS和C#开发一个iPad应用程序,试图创建一个UITableView,其中只有两个角(右上角和右下角)是圆角的。 我知道如何通过设置myTable.Layer.CornerRadius = 6f;来使所有角都变成圆角,但不知道如何仅使其中两个角变成圆角。我已经在SO上搜索过,但只看到了Objective-C的答案。这是我目前的代码:

    private UIView GetModalRowHeaderView2(RectangleF bounds)
    {
        UIView view = new UIView(bounds);
        view.BackgroundColor = UIColor.Gray;

        string[] tableItems = new string[] {"Item One","Item Two","Item Three"};

        UITableView myTable = new UITableView(new RectangleF(0, 20, bounds.Width, bounds.Height - 40), UITableViewStyle.Plain);
        myTable.SeparatorInset = UIEdgeInsets.Zero;
        myTable.ScrollEnabled = false;
        myTable.Source = new TableSource(tableItems);

        // Rounds all corners
        myTable.Layer.CornerRadius = 6f;

        view.Add(myTable);

        return view;
    }

有什么办法可以只将这个元素的两个角变为圆角呢?
谢谢。
2个回答

6

自iOS 11开始,有一个名为的新属性:

例如,3会使得两个顶部角都变成圆角。

view.Layer.MaskedCorners = (CoreAnimation.CACornerMask)3;
view.Layer.CornerRadius = 15f;
  • 0: 没有圆角
  • 1: 左上角
  • 2: 右上角
  • 3: 左右上角(两个角都是上角)
  • 4: 左下角
  • 5: 左上下角(两个角都是左角)
  • 6: 右上下角
  • 7: 左右上角,左下角(除了右下角,其他所有角落)
  • 8: 右下角
  • 9: 左上角,右下角
  • 10: 左右下角(两个角都是右角)
  • 11: 两个上角和右下角(除了左下角,其他所有角落)
  • 12: 左右下角(两个角都是下角)
  • 13: 左右下角和左上角(除了右上角,其他所有角落)
  • 14: 左右下角和右上角(除了左上角,其他所有角落)
  • 15: 所有角落都是圆角

简单的解决方案。谢谢。 - Hanushka Suren

5

您需要使用遮罩层。在我的Github库中,我有一个Xamarin.iOS代码示例,展示了如何创建一个带有圆角的矩形视图(RoundedRectView)。您可以以此为起点。

以下是代码片段:

UIBezierPath maskPath = UIBezierPath.FromRoundedRect (this.Bounds, this.eRoundedCorners, new SizeF (this.fCornerRadius, this.fCornerRadius));

CAShapeLayer maskLayer = new CAShapeLayer ();
maskLayer.Frame = this.Bounds;
maskLayer.Path = maskPath.CGPath;

// Set the newly created shape layer as the mask for the image view's layer
this.Layer.Mask = maskLayer;

谢谢@Krumelur...我会去看看的。 - Gavin Sutherland

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接