【发布时间】:2019-04-27 19:13:41
【问题描述】:
我提前道歉,因为这感觉很简单,但是我已经尝试了几天,但经过大量搜索后无法弄清楚。
当我检查我以编程方式创建的带有约束的图像的高度时,大小与其呈现的大小不匹配。
// Earth Image Setup
view.addSubview(earthImageView)
earthImageView.bottomAnchor.constraint(equalTo: loginTextViewTitle.topAnchor, constant: -standardSpacer).isActive = true
earthImageView.widthAnchor.constraint(equalToConstant: 500).isActive = true
earthImageView.heightAnchor.constraint(equalToConstant: 500).isActive = true
earthImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
// Value of earthImageViewHeight is 276. Why isn't it 500?
let earthImageViewHeight = earthImageView.frame.size.height
我看到 276 来自原始@1x 图像的像素高度。但是,我怎样才能得到实际的渲染高度呢?
我认为这可能与points vs pixels 有关,但似乎也不是这样。
【问题讨论】:
-
可能是因为你检查得太早了。约束是关于布局发生时要做什么的说明。它还没有发生。
-
你还记得把
translates...设置为false吗? -
是的,translatesAutoresizingMaskIntoConstraints = false
-
您是否也将图像视图的
contentMode设置为aspectFit或aspectFill?并将其clipsToBounds设置为true?否则,您将看到整个图像的原始大小。 -
是的,是的。 :)
标签: ios swift nslayoutconstraint