自定义Uiview的子视图什么时候知道自己的界限?
我创建了一个具有一个子视图的自定义Uiview,该子视图包含几个使用布局约束的大小的Square子视图。我希望每个子视图都是圆形的,因此我在数组中持有对视图的引用,然后在layoutsubviews方法中迭代一系列视图,并应用一个角度半径,该转角半径是View.Bounds.height的一半。
是,这仅有70%的时间。有时,视图似乎不知道其界限,并且视图被视为正方形。
主视图,内容视图,然后在“> 我可能正在接近这一切的 ……有人对我有任何建议吗?在哪里可以安全地施加拐角半径,以便它总是作为一个完美的圆圈?
谢谢
I've created a custom UIView with a subview containing several square subviews which are sized using layout constraints. I'd like for each subview to be circular so I'm holding a reference to the view in an array, then iterating through the array of views in the layoutSubviews method and applying a corner radius that is half the view.bounds.height.
The problem is, this only works 70% of the time. Sometimes the view doesn't seem to know its bounds and the views are rendered as square.
I'm probably approaching this all wrong... does anyone have any advice for me? Where is it safe to apply the corner radius so that it always renders as a perfect circle?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
管理此操作的最可靠方法是子类
uiview
,让它处理自己的舍入。例如:
此简单控制器示例:
生成此输出:
The most reliable way to manage this is to subclass
UIView
and let it handle its own rounding.For example:
This simple controller example:
Produces this output:
No need to iterate through subviews and setting values based on frame sizes... all you need to do is set the size/position constraints.
Donmag的示例很酷。此外,您可以这样做,而无需为LayoutsUbview编写代码。您必须在 中添加子视图的约束之后,必须添加此设置
。
Donmag's example is cool.Also, you can do it this way without writing code to layoutsubview. You must add this settings
after adding the constraints
of the subviews infor iteration
.您可以在
layoutsubviews()
方法中应用角半径You can apply the corner radius inside the
layoutSubviews()
method