Search code examples
objective-cuiviewcontrollercgpath

UIViewController.view.superview custom shape


I have a controller presenting as custom shape on iPad. Actually I have made the view itself transperent, but its subviews are visible to the user. I use the following code in viewWillLayoutSubviews method to make a controller with a small rectangle form:

self.view.backgroundColor = [UIColor clearColor];
self.view.superView.backgroundColor = [UIColor clearColor];
self.view.superview.bounds =...

But what if I want not a rectangle? Is there a way not to be making mind blowing CGPath for that purposes?


Solution

  • In that case you have to create a CAShapeLayer with the shape you want and then mask the view's layer with the CAShapeLayer you've created. For example, a view with a circle mask:

    UIBezierPath *aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
                                                         radius:75
                                                     startAngle:0
                                                       endAngle:M_PI * 2
                                                      clockwise:YES];
    
    
    CAShapeLayer *lm = [[CAShapeLayer alloc] init];
    lm.path = aPath.CGPath;
    self.view.layer.mask = lm;