ios 如何仅为 UIView 的左下角、右下角和左上角设置cornerRadius?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25616382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?
提问by Siva
Is there a way to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?
有没有办法只为 UIView 的左下角、右下角和左上角设置cornerRadius?
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;
回答by Justin Boo
You can do it like this:
你可以这样做:
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;
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
并将其作为子图层添加到视图层。像这样(将此代码放在上面的代码下方):
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];
In swift 3.0 like this:
在像这样的 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
回答by Museer Ahamad Ansari
Tested in xcode 8 and swift 3
在 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
}
}
And use like this
并像这样使用
YourView.roundCorners([.topLeft, .bottomLeft], radius: 10)
回答by Shamsudheen TK
Use a custom UIView
and Implement the below code
使用自定义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
Output:
输出:
Look at the UIBezierPath.h
options :
看看UIBezierPath.h
选项:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
回答by metpb
Setting corner-radius for a view in Swift
为视图设置角半径 Swift
The creation and masking of layers should run inside the optional drawRect()
block inherited from UIView
图层的创建和遮罩应在drawRect()
继承自的可选块内运行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
}
回答by Siddhesh Mahadeshwar
I know it's very late, but i just create one method where you can pass only one or multiple UIRectCorners.
我知道现在已经很晚了,但我只是创建了一种方法,您只能在其中传递一个或多个 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;
}
回答by Anurag Bhakuni
hope this will help you
希望能帮到你
your above code running perfectly on my machine , may be you set CAShapeLayer
frame equal to your view frame due to which your view will disappear but i am not seeing this line in your code ,so please check your view property and apply below code
您上面的代码在我的机器上完美运行,可能是您将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;
回答by Den
On iOS 11, What you only need is the maskedCorners.
在 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
}
回答by Vijay Rathod
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;
回答by Hardik Thakkar
Try this code
试试这个代码
-(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;
}
call using
调用使用
[self setMaskTo:myView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
Available options:
可用选项:
UIRectCornerTopLeft, UIRectCornerTopRight,UIRectCornerBottomLeft,UIRectCornerBottomRight,UIRectCornerAllCorners
回答by Maulik Patel
use this code corner radius of view only two sides.after long time i got suggestion ios. here viewFilter is view outlet which we want rounded.
使用此代码角的视野半径只有两侧。很长一段时间后,我得到了 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;
}