2014-09-02 140 views
29

有没有办法为UIView的左下角,右下角和左上角设置cornerRadius?如何为UIView的左下角,右下角和左上角设置cornerRadius?

我尝试了以下方法,但最终导致视图消失。下面的代码有什么问题吗?

UIBezierPath *maskPath; 
    maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(20.0, 20.0)]; 
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
    maskLayer.path = maskPath.CGPath; 
    view.layer.mask = maskLayer; 
+0

尝试使用'UIBezierPath * maskPath = [UIBezierPath新];'然后用'addLineToPoint:','addArcWithCenter:半径:由startAngle:endAngle:顺时针方向:'方法。 – 2014-09-02 05:42:23

+0

你的代码似乎没问题。只需检查您是否添加了QuartzCore框架并在您正在编写此代码的类中导入。我不知道情况确实如此,但早些时候发生在我身上。 – 2014-09-02 06:22:31

+0

可能的重复[Rounded UIView使用CALayers - 只有一些角落 - 如何?](http://stackoverflow.com/questions/2264083/round-uiview-using-calayers-only-some-corners-how) – Stuart 2014-09-02 06:34:53

回答

113

你可以这样说:

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.viewOutlet.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)]; 

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

enter image description here

更新:
如果您需要边框只需创建另一个CAShapeLayer并将其添加到视图的图层作为子图层。像这样(的地方下面上的代码的代码):

CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init]; 
borderLayer.frame = self.view.bounds; 
borderLayer.path = maskPath.CGPath; 
borderLayer.lineWidth = 4.0f; 
borderLayer.strokeColor = [UIColor blackColor].CGColor; 
borderLayer.fillColor = [UIColor clearColor].CGColor; 

[self.viewOutlet.layer addSublayer:borderLayer]; 

enter image description here

在SWIFT 3.0这样的:

let maskPath = UIBezierPath.init(roundedRect: self.viewOutlet.bounds, byRoundingCorners:[.topLeft, .bottomLeft], cornerRadii: CGSize.init(width: 10.0, height: 10.0)) 
let maskLayer = CAShapeLayer() 
maskLayer.frame = self.viewOutlet.bounds 
maskLayer.path = maskPath.cgPath 
self.viewOutlet.layer.mask = maskLayer 
+0

这里我需要设置边界为这个看法...是否有可能显示边界? – Siva 2014-09-02 09:30:43

+0

@Ssn看看更新的答案 – 2014-09-02 10:27:20

+0

谢谢。工作得很好。 – fozoglu 2015-07-14 11:19:04

0

您错过了添加UIRectCornerTopLeft。请试试这个代码:

UIRectCorner rectCorner = UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight; 

UIBezierPath *maskPath; 
maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds 
           byRoundingCorners:rectCorner 
             cornerRadii:CGSizeMake(20.0, 20.0)]; 

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
maskLayer.path = maskPath.CGPath; 
view.layer.mask = maskLayer; 
view.layer.masksToBounds = YES; 

可能会有所帮助....

+3

复制从http ://stackoverflow.com/questions/10167266/how-to-set-cornerradius-for-only-top-left-and-top-right-corner-of-a-uiview?rq = 1 – boeqqsh 2014-09-02 06:04:56

8

使用自定义UIView与实现下面的代码

#import "CustomUIView.h" 

@implementation CustomView 

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    CAShapeLayer * maskLayer = [CAShapeLayer layer]; 
    maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: self.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){20.0, 20.0}].CGPath; 

    self.layer.mask = maskLayer; 
} 

@end 

输出:

enter image description here

看那UIBezierPath.h选项:

typedef NS_OPTIONS(NSUInteger, UIRectCorner) { 
    UIRectCornerTopLeft  = 1 << 0, 
    UIRectCornerTopRight = 1 << 1, 
    UIRectCornerBottomLeft = 1 << 2, 
    UIRectCornerBottomRight = 1 << 3, 
    UIRectCornerAllCorners = ~0UL 
}; 
3

希望这将帮助你

你我的机器上运行完美上面的代码,可能是你设置CAShapeLayer帧等于你的看法由于你的观点将消失,但我没有看到你的代码中的这一行,所以请检查您的视图属性,并应用下面的代码

UIBezierPath *maskPath; 
    maskPath = [UIBezierPath bezierPathWithRoundedRect:viewName.bounds 
            byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight |UIRectCornerTopLeft) 
              cornerRadii:CGSizeMake(20.0, 20.0)]; 
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
    maskLayer.frame =viewName.bounds; 
    maskLayer.path = maskPath.CGPath; 
    viewName.layer.mask = maskLayer; 
3

我知道这是很晚了,但我只是创建一个方法,你可以只传递一个或多个UIRectCorners。

[self setMaskTo:myView byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)]; 

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners 
     { 
      UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds 
                  byRoundingCorners:corners 
                   cornerRadii:CGSizeMake(8.0, 8.0)]; 
      CAShapeLayer *shape = [[CAShapeLayer alloc] init]; 
      [shape setPath:rounded.CGPath]; 
      view.layer.mask = shape; 
    } 
0
UIBezierPath *maskPath; 
maskPath = [UIBezierPath bezierPathWithRoundedRect:someView.bounds 
          byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight) 
            cornerRadii:CGSizeMake(10.0,10.0,5.0, 10.0)]; 


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

试试这个代码

-(void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners { 
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(10.0, 10.0)]; 
    CAShapeLayer* shape = [[CAShapeLayer alloc] init]; 
    [shape setPath:rounded.CGPath]; 
    view.layer.mask = shape; 
} 

使用呼叫

[self setMaskTo:myView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight]; 

可用选项:

UIRectCornerTopLeft, UIRectCornerTopRight,UIRectCornerBottomLeft,UIRectCornerBottomRight,UIRectCornerAllCorners 
6

设置角半径的图Swift

层的创建和掩蔽应从UIView

override func drawRect(rect: CGRect) { 
     super.drawRect(rect) 

     let maskPath = UIBezierPath(roundedRect: self.view.bounds, byRoundingCorners: [.TopLeft, .BottomLeft, .BottomRight], cornerRadii: CGSizeMake(10, 10)) 
     let maskLayer = CAShapeLayer() 
     maskLayer.frame = self.view.bounds 
     maskLayer.path = maskPath.CGPath 
     self.view.layer.mask = maskLayer 
    } 
11

继承了可选drawRect()块内运行测试在xcode的8和swift 3

extension UIView { 
    func roundCorners(corners:UIRectCorner, radius: CGFloat) { 
     let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) 
     let mask = CAShapeLayer() 
     mask.path = path.cgPath 
     self.layer.mask = mask 
    } 
} 

而且使用这样

YourView.roundCorners([.topLeft, .bottomLeft], radius: 10) 
0

使用的角度验证码圆角半径只有两个sides.after很长一段时间,我得到的建议IOS。这里viewFilter是我们想要四舍五入的视图插座。

-(void) viewDidAppear:(BOOL)animated{ 
[super viewDidAppear:animated]; 
[self setMaskTo:viewFilter byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight]; 
} 

-(void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners{ 

UIBezierPath *rounded = [UIBezierPathbezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(20.0, 20.0)]; 

CAShapeLayer *shape = [[CAShapeLayer alloc] init]; 
shape.frame = self.view.bounds; 
[shape setPath:rounded.CGPath]; 
view.layer.mask = shape; 
} 
0

在iOS 11上,你只需要的是maskedCorners。

let cornerRadius: CGFloat = 6.0 
    if #available(iOS 11, *) { 
     productImageView.layer.cornerRadius = cornerRadius 
     productImageView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] 

    } else { 
     let path  = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.topLeft , .topRight], cornerRadii:CGSize(width: cornerRadius, height: cornerRadius)) 
     let maskLayer = CAShapeLayer() 
     maskLayer.frame = bounds 
     maskLayer.path = path.cgPath 
     productImageView.mask    = maskLayer 
     productImageView.layer.masksToBounds = true 
    }