【问题标题】:Should't the height of the frame match the height constraint of an imageview?框架的高度不应该与图像视图的高度约束相匹配吗?
【发布时间】:2016-10-28 22:15:16
【问题描述】:

基本上,我有一个设置图像视图的函数。它位于集合视图中的单元格内。 这是一个方形图像视图,我将其放置在另一个视图的中间。

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

}

【问题讨论】:

  • 你什么时候打电话给setupImageView。如果在单元格有机会进行布局之前调用它,可能会导致这种情况。
  • imageview的内容模式是什么?
  • 我在设置单元格的其余部分后调用该函数,这基本上只是标题视图。 imageview 的内容模式是宽高比的。这可能是个问题吗?

标签: ios swift imageview constraints frame


【解决方案1】:

试试这个:

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

}

【讨论】:

  • 嘿,我把它改成了那个,现在 imageviewHeight.constant/2 像以前一样给我 23.345,但另一个现在给我 23.25。非常接近但仍不准确。不知道为什么
  • 在调用 clipsToBounds 后尝试打印值,看看会发生什么
  • 同样的事情发生
  • 您确定没有使用任何其他约束来覆盖这些设置吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
  • 2013-04-18
相关资源
最近更新 更多