ios - 框架的高度不应该与 ImageView 的高度约束相匹配吗?

标签 ios swift imageview constraints frame

基本上,我有一个设置 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/

相关文章:

ios - CLLocationManager startMonitoringForRegion 会使用 desiredAccuracy 吗?

objective-c - Cocoa 深色模式下的菜单文本颜色

ios - 如何使表格单元格展开和折叠

android - 从图库中选择图像并将其加载到特定 Activity 的 ImageView 中,并加载到另一个 Activity 中的抽屉导航 ImageView 中

java - 缩放 ImageView 的最佳方式

ios - iPhone 模拟器 - 文档目录在哪里?

ios - iOS 设备的 iTunes Connect 统计数据

java - 在 Javafx 中更改图像

ios - 克隆在界面生成器中定义的 uiview?

ios - Swift:使用本地化的百万和十亿文本格式化货币