简体   繁体   English

UIImageView创建额外的宽度/高度约束

[英]UIImageView creates extra width/height constraints

When I debug my views in Xcode, my UIImageView has the following Auto Layout constraints: 当我在Xcode中调试视图时,我的UIImageView具有以下自动布局约束:

在此处输入图片说明

I'm expecting the width and height which are initialised to 6 on the view's setter. 我期望在视图的设置器上将其宽度和高度初始化为6。 At the point of debugging these constraints should be 40 instead of 6. But for some reason there are extra constraints for width and height for (content size) that are actually set to 40. 在调试时,这些约束应该是40而不是6。但是由于某种原因,(内容大小)的宽度和高度还有额外的约束,实际上是设置为40。

This is my imageView 's didSet method for the IBOutlet : 这是我imageViewdidSet的方法IBOutlet

@IBOutlet private weak var imageView: UIImageView! {
    didSet {
        if let widthConstraint = imageView.constraintForLayoutAttribute(.Width)
        {
            widthConstraint.constant = MinimumImageLength
            imageView.layoutIfNeeded()
        }
        else
        {
            let widthConstraint = NSLayoutConstraint(item: imageView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: MinimumImageLength)
            NSLayoutConstraint.activateConstraints([widthConstraint])
        }

        if let heightConstraint = imageView.constraintForLayoutAttribute(.Height)
        {
            heightConstraint.constant = MinimumImageLength
            imageView.layoutIfNeeded()
        }
        else
        {
            let heightConstraint = NSLayoutConstraint(item: imageView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: MinimumImageLength)

            NSLayoutConstraint.activateConstraints([heightConstraint])
        }
    }
}

Which gets a width and height constraint if they exist and set its minimum width. 如果存在,则将获得宽度和高度约束,并设置其最小宽度。 As a note, in my view I have explicitly set these width and height constraints and so my code never enters the else in both width and height. 注意,在我看来,我已经明确设置了这些宽度和高度约束,因此我的代码从不输入宽度和高度的else

To get the right constraint I use the following function: 为了获得正确的约束,我使用以下函数:

extension UIView
{
    func constraintForLayoutAttribute(layoutAttribute: NSLayoutAttribute) -> NSLayoutConstraint?
    {
        let filteredArray = constraints.filter { $0.firstAttribute == layoutAttribute }

        return filteredArray.first
    }
}

Later on I use the same function to get the width/height and change both constants. 稍后,我使用相同的函数获取宽度/高度并更改两个常量。 I call layoutIfNeeded() after updating the width/height constraints. 更新宽度/高度约束后,我调用layoutIfNeeded() (I've also tried debugging by also calling setNeedsUpdateConstraints() with no luck). (我也尝试过通过不幸运地调用setNeedsUpdateConstraints()进行调试)。

Interestingly, constraints work as expected if I test by removing the image from the UIImageView and just set a background colour so I can see what's happening. 有趣的是,如果我通过从UIImageView删除图像并仅设置背景颜色进行测试,约束就会按预期工作,从而可以看到正在发生的事情。

Does anyone know why this might be happening? 有谁知道为什么会这样?

On closer inspection of my function constraintForLayoutAttribute: , the filteredArray property actually consists of several constraints; 在仔细检查我的函数constraintForLayoutAttribute: ,filteredArray属性实际上由几个约束组成。 the width constraint I expect and two other content size constraints. 我期望的宽度限制和其他两个内容大小限制。

I'm not really sure if there's a “fix” to improve my method but I've just decided to create outlets for the width and height constraints and use those instead. 我不确定是否有改进方法的“解决方案”,但我只是决定为宽度和高度约束创建出口,并改用这些出口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM