Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazing corner radius outside color issue

This is code that I added to new view controller:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 100)];
    [contentView setClipsToBounds:YES];
    [contentView setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:contentView];

    [[contentView layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
    [[contentView layer] setBorderWidth:1.0f];
    [[contentView layer] setCornerRadius:5.0f];
    [[contentView layer] setMasksToBounds:YES];
} 

The result:

enter image description here

If look at the corners we can see outside blue pixels:

enter image description here

like image 984
Matrosov Oleksandr Avatar asked Dec 07 '25 21:12

Matrosov Oleksandr


1 Answers

You could use a CAShapeLayer :

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 100)];
    [self.view addSubview:contentView];

    CAShapeLayer *subLayer = [[CAShapeLayer alloc] init];
    [subLayer setFillColor:[UIColor blueColor].CGColor];
    [subLayer setStrokeColor:[UIColor grayColor].CGColor];
    [subLayer setLineWidth:6.0];
    [subLayer setPath:[UIBezierPath bezierPathWithRoundedRect:contentView.bounds cornerRadius:5.0].CGPath];

    [contentView.layer addSublayer:subLayer];
}
like image 139
Kujey Avatar answered Dec 09 '25 15:12

Kujey