Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIBezierPath: roundedRect: byRoundingCorners: cornerRadii: acts weird

I'm trying to make 2 corners of a button round. If I select .TopLeft and .BottomLeft like this:

let bezierDisableAdsPath = UIBezierPath(roundedRect: disableAdsButton.bounds, byRoundingCorners: [UIRectCorner.TopLeft , UIRectCorner.BottomLeft] , cornerRadii: CGSizeMake(4.0, 4.0))

        let maskAdsLayer = CAShapeLayer()
        maskAdsLayer.frame = disableAdsButton.bounds
        maskAdsLayer.path = bezierDisableAdsPath.CGPath
        disableAdsButton.layer.mask = maskAdsLayer

than the code works beautifully.

If I chose .TopRight and . BottomRight like this:

let bezierDisableAdsPath = UIBezierPath(roundedRect: disableAdsButton.bounds, byRoundingCorners: [UIRectCorner.TopRight , UIRectCorner.BottomRight] , cornerRadii: CGSizeMake(4.0, 4.0))

        let maskAdsLayer = CAShapeLayer()
        maskAdsLayer.frame = disableAdsButton.bounds
        maskAdsLayer.path = bezierDisableAdsPath.CGPath
        disableAdsButton.layer.mask = maskAdsLayer

than I see no round corners. What is going on here?

I already tried adding maskAdsLayer as a subview and it doesn't work.

like image 296
potato Avatar asked Aug 10 '15 15:08

potato


1 Answers

If you are doing this in viewDidLoad, the auto layout constraints may not have been applied by this point, and thus the frame of your button may not be at its final value, and thus disableAdsButton.bounds is probably not what you expect it to be at that point. Thus, the corners that are being rounded to the right may not be visible. You can confirm this by logging the bounds of the button at the point this code runs and then look at it again after the views have been laid out.

You can resolve this by deferring this until viewDidAppear or, better, viewDidLayoutSubviews.

like image 125
Rob Avatar answered Oct 16 '22 18:10

Rob