I am wondering if it is possible to clip a view to a Bezier Path. What I mean is that I want to be able to see the view only in the region within the closed Bezier Path. The
You can do this easily by setting your view's layer mask to a CAShapeLayer
.
UIBezierPath *myClippingPath = ...
CAShapeLayer *mask = [CAShapeLayer layer];
mask.path = myClippingPath.CGPath;
myView.layer.mask = mask;
You will need to add the QuartzCore
framework to your target if you haven't already.
In Swift ...
let yourCarefullyDrawnPath = UIBezierPath( .. blah blah
let maskForYourPath = CAShapeLayer()
maskForYourPath.path = carefullyRoundedBox.CGPath
layer.mask = maskForYourPath
Just an example of Rob's solution, there's a UIWebView
sitting as a subview of a UIView
called smoothView
. smoothView
uses bezierPathWithRoundedRect
to make a rounded gray background (notice on right). That works fine.
But if smoothView
has only normal clip-subviews, you get this:
If you do what Rob says, you get the rounded corners in smoothView
and all subviews ...
Great stuff.