arachide
arachide

Reputation: 8066

UIView with round corner and white border

Due to UIProgressHUD need to access private api, so I hope to construct 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

Upvotes: 41

Views: 36179

Answers (6)

KSR
KSR

Reputation: 1709

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

Upvotes: 8

aashi
aashi

Reputation: 55

[view.layer setBorderWidth:2];

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

Upvotes: 0

Linh
Linh

Reputation: 60923

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

Upvotes: 12

Shaik Riyaz
Shaik Riyaz

Reputation: 11462

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

Upvotes: 5

Jonathan.
Jonathan.

Reputation: 55554

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

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

Upvotes: 11

MarkPowell
MarkPowell

Reputation: 16540

Using the same layer object:

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

Upvotes: 83

Related Questions