Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
670 views
in Technique[技术] by (71.8m points)

ios - How to make a UIView with optional rounded corners and border?

I am applying corner radius to a UIView i.e. UIRectCornerTopLeft and UIRectCornerTopRight. When I apply this, the border is gone at the corners. How to avoid this?

This is how I apply border to UIView:

 [self.middleView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(4, 4)];
 self.middleView.layer.borderWidth = 0.5f;
 self.middleView.layer.borderColor = [[UIColor colorWith8BitRed:0 green:0 blue:0 alpha:0.25]

And this is a category I am using for applying optional rounded corners:

   #import "UIView+Roundify.h"

   @implementation UIView (Roundify)
  - (void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii {
   CALayer *tMaskLayer = [self maskForRoundedCorners:corners withRadii:radii];
   self.layer.mask = tMaskLayer;
   }

  - (CALayer*)maskForRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii {
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;

  UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect:
                             maskLayer.bounds byRoundingCorners:corners cornerRadii:radii];
    maskLayer.fillColor = [[UIColor whiteColor] CGColor];
    maskLayer.backgroundColor = [[UIColor clearColor] CGColor];
    maskLayer.path = [roundedPath CGPath];

   return maskLayer;
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try below code it work

Your view which you want to rounded TopLeft and TopRight

   UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)];
   [view1 setBackgroundColor:[UIColor grayColor]];
   [self.view addSubview:view1];

Set Corner as below code

UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:view1.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(5.0, 5.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
view1.layer.mask = maskLayer;

OUTPUT IS:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...