Reputation: 27050
I've a UIView
which is of different width and height that's {width:45,height:37}, when I tried to make it exact circle using cornerRadius
of it's layer, the result was not an exact circle which I want. Is the different between width and height makes this difference? How to get exact circle with this size?
btw, here's my code -
myView.layer.cornerRadius = myView.frame.size.width/2.f;
myView.masksToBounds = YES;
I also tried, myView.layer.cornerRadius = myView.frame.size.width/myView.frame.size.height;
It doesn't make the trick! My more research about this, if myView
will be of same width and height then myView.layer.cornerRadius = myView.frame.size.width/2.f;
would work fine! But somehow I can't make it of same width and height.
Upvotes: 2
Views: 726
Reputation: 27050
As per @sebastienFCT suggestions, I applied this logic to get (little closer) circle shape for myView
.
CGFloat max = MAX(myView.frame.size.width, myView.frame.size.height);
CGFloat min = MIN(myView.frame.size.width, myView.frame.size.height);
CGFloat diff = max - min;
CGFloat borderWidth = max - (diff/2.f);
myView.frame = CGRectMake(myView.frame.origin.x, myView.frame.origin.y, borderWidth, borderWidth);
myView.layer.cornerRadius = borderWidth/2.f;
myView.layer.masksToBounds = YES;
I know it's not a proper way, but for now it helped me to send by first build to the client and made my boss happy!
Upvotes: 1
Reputation: 630
I am not sure it is the best answer, but I would try to reframe
my view and then setup my cornerRadius
:
CGFloat borderWidth = MAX(myView.frame.size.width, myView.frame.size.height);
myView.frame = CGRectMake(myView.frame.origin.x, myView.frame.origin.y, borderWidth, borderWidth);
[myView.layer setCornerRadius:borderWidth/2];
By doing such thing, you could face issue with your interface (the width
or the height
of your view will be bigger and can hide over views) that's why I think there's probably a better practice.
Upvotes: 2