开发者

UIView with round corner and white border

Due to UIProgressHUD need to access private api, so I hope to c开发者_开发问答onstruct an UIView with round corner and white border. I know to make the corner round is:

view.layer.cornerRadius = 5;

But how to make the uiview has round corner and white border at the same time?

Welcome any comment

Thanks interdev


Using the same layer object:

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


Sometimes corner radius with white border does not work properly so I use UIBezierPath and CAShapeLayer.

For make the corner radius

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;

For make the border white

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];

It will work. Hope this help


There are border properties in the layer of the view as well: eg:

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


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


code to get rounded corners and border

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


[view.layer setBorderWidth:2];

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜