如何在Xamarin.iOS中将阴影添加到UIView?

如何在Xamarin.iOS中将阴影添加到UIView?,xamarin.ios,shadow,Xamarin.ios,Shadow,我在Swift中找到了此链接: 但是,当我将解决方案调整为Xamarin时,不会出现阴影 public override void ViewDidLayoutSubviews() { base.ViewDidLayoutSubviews(); UIBezierPath path = new UIBezierPath(); PreviousButton.Layer.MasksToBounds = false; PreviousButton.Layer.Shado

我在Swift中找到了此链接:

但是,当我将解决方案调整为Xamarin时,不会出现阴影

public override void ViewDidLayoutSubviews()
{
    base.ViewDidLayoutSubviews();

    UIBezierPath path = new UIBezierPath();
    PreviousButton.Layer.MasksToBounds = false;
    PreviousButton.Layer.ShadowColor = UIColor.Gray.CGColor;
    PreviousButton.Layer.ShadowOffset = new CoreGraphics.CGSize(0f, 7f);
    PreviousButton.Layer.ShadowOpacity = 1;
    PreviousButton.Layer.ShadowPath = path.CGPath;
}

我正在使用自动布局。

原因:

您似乎忘记设置路径的Rect

解决方案1:

您可以直接设置按钮的阴影

public override void ViewDidLayoutSubviews()
{
    base.ViewDidLayoutSubviews();

    PreviousButton.Layer.MasksToBounds = false;
    PreviousButton.Layer.ShadowColor = UIColor.Gray.CGColor;
    PreviousButton.Layer.ShadowOffset = new CoreGraphics.CGSize(0f, 7f);
    PreviousButton.Layer.ShadowOpacity = 1;   
}
解决方案2: 如果确实要使用BezierPath,请设置其矩形

public override void ViewDidLayoutSubviews()
{
    base.ViewDidLayoutSubviews();
    UIBezierPath path = UIBezierPath.FromRect(PreviousButton.Bounds);
    PreviousButton.Layer.MasksToBounds = false;
    PreviousButton.Layer.ShadowColor = UIColor.Gray.CGColor;
    PreviousButton.Layer.ShadowOffset = new CoreGraphics.CGSize(0f, 7f);
    PreviousButton.Layer.ShadowOpacity = 1;
    PreviousButton.Layer.ShadowPath = path.CGPath;   
}

谢谢,现在可以用了。是的,我错过了FromRect实现。