天天看點

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      //四個邊角

希望對你有幫助!