UIImageView不尊重自动布局

时间:2017-10-21 21:50:28

标签: ios uiimageview autolayout

我设置1 2 3 就像这样:

UIImageView

它变为325x325而不是140x140。怎么样?至少控制台应该打印它会破坏一些约束吗?

查看“调试视图层次结构”的外观:enter image description here

let imageSize: CGFloat = 140 let profileImage = RoundImageView() profileImage.image = UIImage(named: "girl_0") profileImage.clipsToBounds = true profileImage.translatesAutoresizingMaskIntoConstraints = false profileImage.contentMode = .scaleAspectFill profileImage.layer.masksToBounds = true profileImage.layer.borderColor = UIColor.GSBackground().cgColor profileImage.layer.borderWidth = 4.0 addSubview(profileImage) profileImage.topAnchor.constraint(equalTo: topAnchor, constant: 18).isActive = true profileImage.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true profileImage.heightAnchor.constraint(equalToConstant: imageSize).isActive = true profileImage.widthAnchor.constraint(equalToConstant: imageSize).isActive = true

我尝试将两个轴的<...RoundImageView: 0x11903e470; baseClass = UIImageView; frame = (117.5 18; 325 325); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1d023a700>>设置为1000,但不起作用。

我已经尝试在[{1}}中为profileImage.setContentHuggingPriority()返回140x140,但没关系,它仍然是325x325(但是说intristicContentSize

我已尝试UIImageView而不是140 (content size)

我只是感到困惑。

完整披露,这里是.scaleAspectFit

.scaleAspectFill

1 个答案:

答案 0 :(得分:0)

我找到了答案。在一个地方 - 我更新了视图,我有profileImage.sizeToFit(),这使得所有约束都相同,但图像变大了。

左图是第一个布局 - 在sizeToFit中调用UIImageView后的第二个布局。

enter image description here enter image description here