YogevSitton
YogevSitton

Reputation: 10108

Rounded views not working in iOS 9

I have a code that takes a view and rounds it corners - turning it into a circle:

imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)];
imageView.layer.cornerRadius = imageView.frame.size.width/2;
imageView.layer.masksToBounds = YES;

This works great on iOS 7 and iOS 8 - but doesn't work on iOS 9. How can I fix it?

Upvotes: 9

Views: 2976

Answers (5)

eilas
eilas

Reputation: 392

Thanks to @YogevSitton answer I've searched over docs and found this:

It appears to be something in apple docs:

By default, the corner radius does not apply to the image in the layer’s contents property; it applies only to the background color and border of the layer.

So, to set cornerRadius to UIImageView it needs to have backgroundColor.

Upvotes: 1

Steven Wong
Steven Wong

Reputation: 21

I jsut subClass UIImageView ,like this ,I use PureLayout control the imageView,like below:

[imageViewIcon autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.contentView withOffset:20];
[imageViewIcon autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.contentView withOffset:10];
[imageViewIcon autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.contentView withOffset:-10];
[imageViewIcon autoMatchDimension:ALDimensionHeight toDimension:ALDimensionWidth ofView:imageViewIcon];




-(instancetype)init
{
if (self = [super init]) {

}

return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
[self layoutIfNeeded];

self.layer.masksToBounds = YES;
self.layer.cornerRadius = self.width*0.5;



}

Upvotes: 0

YogevSitton
YogevSitton

Reputation: 10108

Turns out adding a background color to the ImageView fixes the issue...

Upvotes: 9

Rohit Kumar
Rohit Kumar

Reputation: 887

Try adding Quartzcore.framework in Linked Framework and Libraries

then importing the same in your viewController.m file

Upvotes: 0

Magyar Miklós
Magyar Miklós

Reputation: 4272

If this is not working for anymore, make a component. Subclass from UIView. Do this changes on this subclass:

subclass.layer.cornerRadius = subclass.frame.size.width/2;
subclass.layer.masksToBounds = YES;

and add the imageView to this view. This will solve the problem.

@implementation CircleImageView{

    UIImageView* imageView;
    CGFloat width;

}

- (instancetype) initWithTopLeft:(CGPoint)topLeft width:(CGFloat)wdth{

    self = [super initWithFrame:CGRectMake(topLeft.x, topLeft.y, wdth, wdth)];
    if (self) {
        width = wdth;

        [self createContent];

        self.layer.cornerRadius = width/2.;
        self.clipsToBounds = YES;
    }
    return self;
}

- (void) createContent{
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, width)];
    [self addSubview:imageView];

}

Upvotes: 0

Related Questions