简体   繁体   中英

UIImageView creates extra width/height constraints

When I debug my views in Xcode, my UIImageView has the following Auto Layout constraints:

在此处输入图片说明

I'm expecting the width and height which are initialised to 6 on the view's setter. 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.

This is my imageView 's didSet method for the 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.

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. (I've also tried debugging by also calling setNeedsUpdateConstraints() with no luck).

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.

Does anyone know why this might be happening?

On closer inspection of my function constraintForLayoutAttribute: , the filteredArray property actually consists of several constraints; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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