corner radius on a uiview not working as expected

2019-08-25 02:45发布

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

3条回答
孤傲高冷的网名
2楼-- · 2019-08-25 03:21

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

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

查看更多
爷、活的狠高调
3楼-- · 2019-08-25 03:22

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

override func viewDidLayoutSubviews() {
    self.prepareUI();
}
查看更多
神经病院院长
4楼-- · 2019-08-25 03:24

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();
}
查看更多
登录 后发表回答