简体   繁体   English

UIView的cornerRadius不会平均影响视图

[英]UIView's cornerRadius does not affect view evenly

I am applying cornerRadius normally to a custom view as such: 我通常将cornerRadius应用于这样的自定义视图:

self.layer.cornerRadius = self.frame.size.height/2

However, I noticed that the resultant corner on the UIView is not evenly applied. 但是,我注意到UIView上的合成角未均匀地应用。 Specifically, as seen in the attached photo, the top starts the fade into the corner earlier than the bottom part. 具体来说,如随附的照片所示,顶部比底部早开始淡入角。 While this is zoomed in, the uneven cornering is visible even at normal viewing level. 放大后,即使在正常观看水平下,也可以看到不均匀的转弯。 在此处输入图片说明

Has anyone also encountered this issue was able to resolve it? 有没有人也遇到过这个问题能够解决?

Where do you call this line? 您在哪里叫这条线?

self.layer.cornerRadius = self.frame.size.height/2

This bug usually happens if cornerRadius is not half of FINAL size. 如果cornerRadius不是FINAL大小的一半,通常会发生此错误。 eg: you set this in viewDidLoad, the height of self is 100, the cornerRadius is now 50, but after self finished layout, its height become 90, the cornerRadius is still 50. 例如:您在viewDidLoad中设置此值,自身的高度为100,cornerRadius现在为50,但是在自完成布局后,其高度变为90,cornerRadius仍然为50。

There are 2 ways to solve this: 有两种解决方法:

  1. If you use fixed constraint for height, you can set cornerRadius anywhere you want: self.layer.cornerRadius = self.someHeightConstraint.constraint / 2 如果对高度使用固定约束,则可以在任意位置设置cornerRadius:self.layer.cornerRadius = self.someHeightConstraint.constraint / 2

  2. If you don't, set cornerRadius after the view finishes layout (in its superview's viewDidLayoutSubviews ) 如果不这样做,则在视图完成布局后(在其超级视图的viewDidLayoutSubviews )设置cornerRadius

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM