基本上,我有一个设置 ImageView 的函数。它位于 Collection View 的单元格内。 这是一个正方形 ImageView ,我将其放置在另一个 View 的中间。
问题是出于某种原因,我对高度施加的约束似乎与 .frame.height 不匹配。
我为我的高度限制属性做了一个导出。我将它设置为单元格的高度 * 0.10。
当我打印 imageviewHeight.constant/2 时,我得到 23.345。当我打印 self.imageview.frame.height/2 时,我得到 25。
框架的高度不应该符合高度限制吗?
private func setupImageView() {
imageviewHeight.constant = self.frame.height * 0.10
imageviewWidth.constant = imageviewHeight.constant
imageviewtopconstraint.constant = -(imageviewHeight.constant * 0.50)
imageviewleftconstraint.constant = imageviewWidth.constant - (imageviewHeight.constant * 0.50)
self.updateConstraints()
// imageview is the outlet to the UIImageView
print(imageviewHeight.constant/2)
print(self.imageview.frame.height/2)
self.imageview.layer.cornerRadius = imageviewHeight.constant / 2
self.imageview.clipsToBounds = true
}
最佳答案
试试这个:
private func setupImageView() {
imageviewHeight.constant = self.frame.height * 0.10
imageviewWidth.constant = imageviewHeight.constant
imageviewtopconstraint.constant = -(imageviewHeight.constant * 0.50)
imageviewleftconstraint.constant = imageviewWidth.constant - (imageviewHeight.constant * 0.50)
self.view.layoutIfNeeded()
// imageview is the outlet to the UIImageView
print(imageviewHeight.constant/2)
print(self.imageview.frame.height/2)
self.imageview.layer.cornerRadius = imageviewHeight.constant / 2
self.imageview.clipsToBounds = true
}
关于ios - 框架的高度不应该与 ImageView 的高度约束相匹配吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40313920/