Balint Fodor
Balint Fodor

Reputation: 143

How to add shadow to an UIView in Xamarin.iOS?

I found this link for doing it in Swift: What's the best way to add a drop shadow to my UIView

However when I adapt the solution to Xamarin, no shadow appears.

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;
}

I am using AutoLayout.

Upvotes: 0

Views: 861

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

Cause:

You seem forgot to set the Rect of the path

Solution 1:

You could directly set the shadow of the button

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;   
}

Solution 2: If you do want to use BezierPath , set the rect of it .

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;   
}

Upvotes: 1

Related Questions