1

I refer the post to build the UIView and UIButton corner radius.

But I found when I set the trailing space, the right part corner always not become arc.

My code like below:

 - (void)viewDidLoad {
     [super viewDidLoad];

     self.btn = (UIButton*)[self roundCornerOnView:self.btn onTopLeft:YES topRight:YES bottomLeft:YES bottomRight:YES radius:5.0];
     self.myView = (UIView*)[self roundCornerOnView:self.myView onTopLeft:YES topRight:YES bottomLeft:YES bottomRight:YES radius:5.0];

 }

 -(UIView*) roundCornerOnView:(UIView*)view onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius
 {
     if( tl || tr || bl || br )
     {
         UIRectCorner corner = 0;
         if (tr)
         {
             corner = corner | UIRectCornerTopRight;
         }
         if (br)
         {
             corner = corner | UIRectCornerBottomRight;
         }


         if( tl )
         {
             corner = corner | UIRectCornerTopLeft;
         }
         if (bl)
         {
             corner = corner | UIRectCornerBottomLeft;
         }

         UIView *roundedView = view;
         UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
         CAShapeLayer *maskLayer = [CAShapeLayer layer];
         maskLayer.frame = roundedView.bounds;
         maskLayer.path = maskPath.CGPath;
         roundedView.layer.mask = maskLayer;
         return roundedView;
     }
     else
     {
         return view;
     }

 }

My layout like below:

The button layout property: enter image description here

The view layout property: enter image description here

The right part always right angle like below.
enter image description here

How can I resolve this problem about the elements set the trailing space and with the corner radius?

Community
  • 1
  • 1
dickfala
  • 3,246
  • 3
  • 31
  • 52

2 Answers2

4

You should update the frame of mask layer inside layoutSubviews method of your views or inside viewDidLayoutSubviews method of your view controller.

Because when you set mask layer's frame, the view has a different frame. After autolayout updates the frames and layouts subviews, the view has the correct frame value. And probably autolayout operates at UIView level, not on CALayer level. Therefore you should update mask layer's frame after layout phase.

Mert Buran
  • 2,989
  • 2
  • 22
  • 34
  • thanks. Try to add in viewDidLayoutSubviews, it correct. – dickfala Mar 17 '15 at 15:00
  • Glad it worked. For the sake of completeness, I suggest creating and adding mask layer inside viewDidLoad and then just updating its frame inside viewDidLayoutSubviews. Because as orientation changes, viewDidLayoutSubviews can be called again and you don't want to create and add mask layer twice. – Mert Buran Mar 17 '15 at 15:03
  • But in the roundCornerOnView method have roundedView.layer.mask = maskLayer; in there. They are called from viewdidload. How to add mask layer inside viewDidLoad another? thanks. – dickfala Mar 17 '15 at 15:24
  • basically you need to do this is the following viewDidLoad: create and add mask layer viewDidLayoutSubviews: access to mask layer and modify its frame you can write new methods or change the current one for this. – Mert Buran Mar 17 '15 at 15:31
0

Why don't you just use

- (void)viewDidLoad {
    [super viewDidLoad];
    self.btn.layer.cornerRadius = 5;
    self.myView.layer.cornerRadius = 5;
}

You can also set these on IB via User Defined Runtime Attributes

Fabio Felici
  • 2,841
  • 15
  • 21