如何在TableView中向部分添加阴影

时间:2020-09-01 04:45:52

标签: c# ios xamarin.ios

Please refer this screenshot for the design

  //Top Left Right Corners
    var maskPathTop = UIBezierPath.FromRoundedRect(cell.Bounds, UIRectCorner.TopLeft | UIRectCorner.TopRight, new CoreGraphics.CGSize(8, 8));
    var shapeLayerTop = new CAShapeLayer();
    shapeLayerTop.Frame = cell.Bounds;
            shapeLayerTop.Path = maskPathTop.CGPath;

            //Bottom Left Right Corners
            var maskPathBottom = UIBezierPath.FromRoundedRect(cell.Bounds, UIRectCorner.BottomLeft | UIRectCorner.BottomRight, new CoreGraphics.CGSize(8, 8));
    var shapeLayerBottom = new CAShapeLayer();
    shapeLayerBottom.Frame = cell.Bounds;
            shapeLayerBottom.Path = maskPathBottom.CGPath;

            //All Corners
            var maskPathAll = UIBezierPath.FromRoundedRect(cell.Bounds, UIRectCorner.BottomLeft | UIRectCorner.BottomRight | UIRectCorner.TopLeft | UIRectCorner.TopRight, new CoreGraphics.CGSize(8, 8));
    var shapeLayerAll = new CAShapeLayer();
    shapeLayerAll.Frame = cell.Bounds;
            shapeLayerAll.Path = maskPathAll.CGPath;

            if (indexPath.Row == 0 && indexPath.Row == tableView.NumberOfRowsInSection(indexPath.Section) - 1)
            {
                cell.Layer.Mask = shapeLayerAll;
            }
            else if (indexPath.Row == 0)
            {
                cell.Layer.Mask = shapeLayerTop;

            }

            else if (indexPath.Row == tableView.NumberOfRowsInSection(indexPath.Section) - 1)
            {
                cell.Layer.Mask = shapeLayerBottom;
            }

我已经尝试过为每个部分获取圆角,但是在这种情况下,我无法为每个部分添加阴影。因为我是Xamarin iOS的初学者,所以谁能在C#中提出建议。 预先感谢!

1 个答案:

答案 0 :(得分:0)

创建一个新的shadowViewshadowView.AddSubview(cell);,下面是代码示例:

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();
    // Perform any additional setup after loading the view, typically from a nib.

    UILabel cell = new UILabel();
    cell.BackgroundColor = UIColor.Blue;
    cell.Text = "test";

    UIView shadowView = new UIView(new CoreGraphics.CGRect(100, 100, 200, 100));
    shadowView.Layer.ShadowColor = UIColor.Black.CGColor;
    shadowView.Layer.ShadowRadius = 8.0f;
    shadowView.Layer.ShadowOffset = new CGSize(13.0, 13.0);
    shadowView.Layer.ShadowOpacity = 0.5f;

    cell.Frame = shadowView.Bounds;

    //Top Left Right Corners
    var maskPathTop = UIBezierPath.FromRoundedRect(cell.Bounds, UIRectCorner.TopLeft | UIRectCorner.TopRight, new CoreGraphics.CGSize(8, 8));
    var shapeLayerTop = new CAShapeLayer();
    shapeLayerTop.Frame = cell.Bounds;
    //shapeLayerTop.BorderWidth = 3;
    //shapeLayerTop.BorderColor = VM.RedColor.ToNativeColor().CGColor;
    shapeLayerTop.Path = maskPathTop.CGPath;

    //Bottom Left Right Corners
    var maskPathBottom = UIBezierPath.FromRoundedRect(cell.Bounds, UIRectCorner.BottomLeft | UIRectCorner.BottomRight, new CoreGraphics.CGSize(8, 8));
    var shapeLayerBottom = new CAShapeLayer();
    shapeLayerBottom.Frame = cell.Bounds;
    //shapeLayerBottom.BorderColor = VM.RedColor.ToNativeColor().CGColor;
    //shapeLayerBottom.BorderWidth = 3;
    shapeLayerBottom.Path = maskPathBottom.CGPath;

    //All Corners
    var maskPathAll = UIBezierPath.FromRoundedRect(cell.Bounds, UIRectCorner.BottomLeft | UIRectCorner.BottomRight | UIRectCorner.TopLeft | UIRectCorner.TopRight, new CoreGraphics.CGSize(8, 8));
    var shapeLayerAll = new CAShapeLayer();
    shapeLayerAll.Frame = cell.Bounds;
    //shapeLayerAll.BorderWidth = 3;
    //shapeLayerAll.BorderColor = VM.RedColor.ToNativeColor().CGColor;
    shapeLayerAll.Path = maskPathAll.CGPath;

    cell.Layer.Mask = shapeLayerAll;

    shadowView.AddSubview(cell);
    View.Add(shadowView);

}

引用:UIView with rounded corners and drop shadow?