UIView圆角和白色边框

时间:2010-12-01 14:00:19

标签: iphone

由于UIProgressHUD需要访问私有api, 所以我希望构建一个带圆角和白色边框的UIView。 我知道要转角是:

view.layer.cornerRadius = 5;

但如何让uiview同时具有圆角和白色边框?

欢迎任何评论

由于 InterDev中

6 个答案:

答案 0 :(得分:78)

使用相同的图层对象:

view.layer.borderWidth = 1;
view.layer.borderColor = [[UIColor whiteColor] CGColor];

答案 1 :(得分:11)

视图层中也有边框属性:例如:

view.layer.borderWidth = 1;
view.layer.borderColor = [UIColor redColor].CGColor;

答案 2 :(得分:11)

有时带有白色边框的角半径无法正常使用所以我使用UIBezierPathCAShapeLayer

制作角半径

UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.imageView.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
self.imageView.layer.mask = maskLayer;

使边框变白

CAShapeLayer*   borderShape = [CAShapeLayer layer];
borderShape.frame = self.imageView.bounds;
borderShape.path = maskPath.CGPath;
borderShape.strokeColor = [UIColor whiteColor].CGColor;
borderShape.fillColor = nil;
borderShape.lineWidth = 3;
[self.imageView.layer addSublayer:borderShape];

它会起作用。希望这个帮助

答案 3 :(得分:5)

view.layer.cornerRadius = 5;
view.clipsToBounds = YES;
view.layer.borderWidth = 1;
view.layer.borderColor = [UIColor whiteColor].CGColor;

答案 4 :(得分:4)

获取圆角和边框的代码

#import <QuartzCore/QuartzCore.h>
view.layer.cornerRadius = 10;
view.layer.borderWidth = 1;
view.layer.borderColor = [[UIColor whiteColor] CGColor];

答案 5 :(得分:0)

[view.layer setBorderWidth:2];

[view.layer setBorderColor:[[UIColor whiteColor]CGColor]];