本文介绍了如何设置cornerRadius只用于UIView的左下角,右下角和左上角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法只为UIView的左下角,右下角和左上角设置cornerRadius?
Is there a way to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?
我尝试了以下方法,但它结果让视图消失了。下面的代码有什么问题吗?
I tried the following, but it ended up making the view disappear. Is there anything wrong with the code below?
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;
推荐答案
你可以这样做:
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;
更新:
如果您需要边框,只需创建另一个 CAShapeLayer
并将其作为子图层添加到视图层。像这样(将此代码放在上面的代码下面):
Update:
If You need border just create another CAShapeLayer
and add it to view's layer as sublayer. Like this (place this code below upper code):
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];
在swift 3.0中如下:
In swift 3.0 like this:
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
这篇关于如何设置cornerRadius只用于UIView的左下角,右下角和左上角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!