SRT
SRT

Reputation: 13

corner radius on a uiview not working as expected

cornerRadius = (view.frame.size.height / 2.0)

Doesn't seem to work as expected. How to solve this? What might be the issue?

Output Screen-

My code and simulator output is shown in this image. Click Here

Upvotes: 1

Views: 2576

Answers (3)

Sonu Malik
Sonu Malik

Reputation: 11

you only need to set clipToBounds true for your View.
Example;

let view = UIView()
view.layer.cornerRadius = 14;
view.clipsToBounds = true;

Upvotes: 0

Terje
Terje

Reputation: 1000

I'm guessing that you want your corner radiuses to meet each other, and not have a straight line vertically between them? So that the sides of your red and green buttons meet up in a half circle?

Your view constraints aren't actually resolved until the viewDidLayoutSubviews() function, so if your view changes during runtime because of the constraints you have set up, the frame you are calculating your corner radiuses from will change after viewDidLoad. If you use viewDidLayoutSubviews() instead you should be fine:

override func viewDidLayoutSubviews() {
    self.prepareUI();
}

Upvotes: 0

trungduc
trungduc

Reputation: 12144

My solution in this case is you should put prepareUI method in viewDidLayoutSubviews. Try to add the code below.

override func viewDidLayoutSubviews() {
    self.prepareUI();
}

Upvotes: 1

Related Questions