【发布时间】:2016-10-16 17:25:12
【问题描述】:
我有一个UIview,它的顶部有一个UIImageView。我希望两者都被舍入,所以我写了这些代码行:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// make rounded profile image
self.avatarPlaceholderView.layer.masksToBounds = true
self.avatarPlaceholderView.layer.cornerRadius = self.avatarPlaceholderView.frame.width / 2.0
self.avatarImageView.layer.masksToBounds = true
self.avatarImageView.layer.cornerRadius = self.avatarImageView.frame.width / 2.0
}
在纸上一切都应该正常工作,但事实并非如此。虽然UIView 变为四舍五入,但UIImageView 根本不显示。进一步调查显示self.avatarPlaceholderView.frame.width 返回正确的宽度,但self.avatarImageView.frame.width 返回宽度为 1000。更奇怪的是,如果我在最后两行注释,UIImageView 将可见,因此约束设置正确。
我不知道为什么会这样。
编辑
故事板屏幕截图:
【问题讨论】:
-
显示更多信息,视图的约束/层次结构等。
-
继承
UIImageView并使用layoutSubviews要容易得多。此外,您只需更改情节提要中的类,就不需要更多代码了。 -
您确定您正在使用正确的尺寸等级运行您的应用程序吗?我看到您在视图中标记了
This Size Class。All在那里不是更有意义吗? -
将其更改为全部,无效。顺便说一下,这个尺寸等级是默认值。我也可能找到另一种方法来做到这一点,但我真的很好奇为什么这不起作用,而且无论如何它应该
-
标签: ios swift uiimageview nslayoutconstraint