【发布时间】:2017-10-10 20:44:44
【问题描述】:
我正在创建一个具有以下约束的简单 UIImageView:
self.view.addConstraint(imageView.topAnchor.constraint(equalTo: contentLayoutGuide.topAnchor))
self.view.addConstraint(imageView.centerXAnchor.constraint(equalTo: contentLayoutGuide.centerXAnchor))
self.view.addConstraint(imageView.heightAnchor.constraint(equalTo: contentLayoutGuide.heightAnchor))
self.view.addConstraint(imageView.widthAnchor.constraint(equalTo: contentLayoutGuide.heightAnchor))
重要的是最后一个,它设置宽度等于图像视图的高度,图像视图的高度由顶部和底部锚点定义。
但是,当设置imageView.image 时,图像会遍布屏幕并且太大了。
这个调试过程修复了它:
po for cn in self.imageView!.constraints { cn.isActive = false }
po CATransaction.flush()
这使图像按预期放入 imageView 中,但直到此时,imageView 都搞砸了。
有趣的是,在设置图像之前,imageView 本身没有任何约束,然后设置图像会创建这两个:
- 0 : <NSContentSizeLayoutConstraint:0x6040002b2120 UIImageView:0x7fa98f757b90.width == 488 Hug:250 CompressionResistance:750 (active)>
- 1 : <NSContentSizeLayoutConstraint:0x6040002b2180 UIImageView:0x7fa98f757b90.height == 487 Hug:250 CompressionResistance:750 (active)>
这些限制似乎打破了它,因为禁用它们可以解决问题。它们仅在设置图像时添加
【问题讨论】:
-
您是否在
imageView上将translatesAutoresizingMaskIntoConstraints设置为false? -
是的,当然。否则默认情况下约束会中断
-
您是否将图像视图的内容模式设置为宽高比适合?
标签: ios swift uiimageview