美文网首页iOS那些事iOS
iOS开发设置view某几个角为圆角

iOS开发设置view某几个角为圆角

作者: MichealXXX | 来源:发表于2018-08-24 16:37 被阅读0次
有时候为了设计的美观我们需要将一些控件的某几个角设置为圆角,很多朋友就会开始挠头不知道怎么去弄,我们知道设置四个角都为圆角很简单,创建一个view,设置其layer.cornerRadius即可,代码如下:
    UIView *testview = [[UIView alloc] init];
    testview.layer.cornerRadius = 10;
    [self.view addSubview: testview];
其实指定圆角也是通过view的layer属性来设置的,我通过设置控件的上面两个角为圆角来举例,代码如下:
    UIView *testview = [[UIView alloc] init];
    [self.view addSubview: testview];

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: testview.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10,10)];
    //创建 layer
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = testview.bounds;
    //赋值
    maskLayer.path = maskPath.CGPath;
    testview.layer.mask = maskLayer;
首先创建view,然后单独设置其layer的方法,再将其赋值给view的layer属性即可,通过方法里面的参数UIRectCornerTopLeft,UIRectCornerTopRight我们便可以看出这是设置其左上角以及右上角为圆角,在cornerRadii:中设置圆角尺寸即可实现我们想要的效果。

相关文章

网友评论

    本文标题:iOS开发设置view某几个角为圆角

    本文链接:https://www.haomeiwen.com/subject/aqqiiftx.html