Search code examples
objective-cautolayoutcalayer

Circle shaped, dynamic view with AutoLayout


Before AutoLayout I could do a view cut to a circle shape with setting .layer.cornerRadius to half of the view's height.

Now, using AutoLayout how can I achieve, to my view keep look like a circle?

I have already tried and failed:

  • using KVO to find out when frame changes. It gets called, but at that point setting cornerRadius on the view does not have any effect
  • calling -setNeedsLayout to have frame values before I set cornerRadius (does not work either)

Solution

  • You need to set the property masksToBounds on the layer.

    xyz.layer.masksToBounds = YES;
    

    Further, the reason why this actually won't affect Auto Layout is because the view's frame will remain the same regardless of its corner radius.