天天看点

iOS 给控件加圆角

这里以UIButton控件为例子,真接上代码了!

    UIButton *helpBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

方法一:

    helpBtn.layer.cornerRadius =5.0; //圆角半径为5.0

    helpBtn.layer.masksToBounds =YES;

这样写会将helpBtn的四个角都加上圆角,可不可以只将指定的边角设置为圆角呢?可以 !

方法二:

    UIBezierPath *maskPath = [UIBezierPathbezierPathWithRoundedRect:helpBtn.boundsbyRoundingCorners:

                                                                                         UIRectCornerTopLeft |UIRectCornerTopRight

                                                                                         cornerRadii :CGSizeMake ( 4.0f , 4.0f )];

    CAShapeLayer *maskLayer = [[CAShapeLayeralloc]init];

    maskLayer.frame = helpBtn.bounds;

    maskLayer.path = maskPath.CGPath;

    helpBtn.layer.mask = maskLayer;

    以上代码是将helpBtn的左上和右上的边角设置为半径为4.0的圆角。

    UIRectCorner有以下四种属性:

    UIRectCornerTopLeft             //左上

    UIRectCornerTopRight          //右上  

    UIRectCornerBottomLeft       //左下

    UIRectCornerBottomRight    //右下

    UIRectCornerAllCorners      //四个边角

希望对你有帮助!