框架的高度是否应该与图像视图的高度约束相匹配?

时间:2016-10-28 22:15:17

标签: ios swift imageview constraints frame

基本上,我有一个设置图像视图的功能。它位于collectionview中的单元格内部。 这是一个正方形的图像视图,我将其放在另一个视图的中间位置。

问题在于,出于某种原因,我对高度的约束似乎与.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

}

1 个答案:

答案 0 :(得分:0)

试试这个:

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

}