Search code examples
iosswiftconstraintsuipangesturerecognizer

Unknow constrain found inside a view


I'm creating a custom class to make a view expandable; the class works with a UIPanGestureRecognizer that modifies the view's height constraints. While debugging the code I used the following code to show the height constraints associated with the view.

print(self.constraints.filter({ $0.firstAttribute == .height}).map{print("costraints \($0)")})

The strange thing is that in addition to the costrains created by the method to expand the view there's one more costrain that I have no idea where comes frome. Here's what's printed in the console:

constraint <NSAutoresizingMaskLayoutConstraint:0x6140002828f0 h=--& v=--& UIStackView:0x7ff1c871d0e0.height == 190   (active)>
constraint <NSLayoutConstraint:0x60800009c160 PhotoMapping.ExpandableView:0x7ff1c8416fa0.height == 100   (active)>

Here's the complete code for the class.

import Foundation
import UIKit

class ExpandableView: UIView {
    let panGestureRecognizer = UIPanGestureRecognizer()
    var minHeight: CGFloat = 0 {
        didSet {
            initialHeight = minHeight
        }
    }
    var maxHeight: CGFloat = 0
    var initialHeight: CGFloat = 0
    var initialPoint: CGPoint? = nil

    var heightConstraintConstant: CGFloat {
        get {
            if !self.constraints.filter({ $0.firstAttribute == .height}).isEmpty {
                return self.constraints.filter({$0.firstAttribute == .height && $0.secondItem == nil}).first!.constant
            } else {
                let constrain = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: initialHeight)
                self.addConstraint(constrain)
                return constrain.constant
            }
        }

        set(newConstant){
            if !self.constraints.filter({ $0.firstAttribute == .height}).isEmpty {
                self.constraints.filter({$0.firstAttribute == .height && $0.secondItem == nil}).first?.constant = newConstant
                self.constraints.filter({$0.firstAttribute == .height && $0.secondItem == nil}).map{print("constant \($0.constant)")}
            } else {
                let constrain = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: newConstant)
                self.addConstraint(constrain)
            }
            self.layoutIfNeeded()
        }

    }

    func initialize(minHeight: CGFloat, maxHeight: CGFloat) {
        panGestureRecognizer.addTarget(self, action: #selector(handlePan(_:)))
        panGestureRecognizer.minimumNumberOfTouches = 1
        panGestureRecognizer.maximumNumberOfTouches = 1
        self.addGestureRecognizer(panGestureRecognizer)
        self.minHeight = minHeight
        self.maxHeight = maxHeight
    }

    @objc func handlePan(_ sender: Any) {
        let translation = panGestureRecognizer.translation(in: self)

        if initialPoint == nil {
            initialPoint = translation
        }

        let translationHeight = CGFloat(translation.y - initialPoint!.y)
        print("translationHeight: \(translationHeight)")

        if panGestureRecognizer.state == .changed {
            let finalHeight = translationHeight + initialHeight
            print("finalHeight: \(finalHeight)")
            if finalHeight <= maxHeight && finalHeight >= minHeight {
                heightConstraintConstant = finalHeight
            }
        } else if panGestureRecognizer.state == .ended {

            let mediumY = maxHeight / 2 + minHeight / 2

            if translation.y <= 0 {
                heightConstraintConstant = minHeight
            } else if translationHeight >= maxHeight {
                heightConstraintConstant = minHeight
            } else if heightConstraintConstant >= mediumY {

                UIView.animate(withDuration: 0.4) {
                    self.heightConstraintConstant = self.maxHeight
                    self.layoutIfNeeded()
                }

            } else if heightConstraintConstant <= mediumY {

                UIView.animate(withDuration: 0.2) {
                    self.heightConstraintConstant = self.minHeight
                    self.layoutIfNeeded()
                }
            }

            initialPoint = nil
            initialHeight = heightConstraintConstant
        }

        layoutIfNeeded()
    }

}

I set the view's class in the storyBoard as "ExpandableView" and I initialized it inside the superclass viewController as shown below: And here's the code inside the ViewController where I initialize the view called profileView.

profileView.isUserInteractionEnabled = true
profileView.initialize(minHeight: 100, maxHeight: 190)

Solution

  • Found the bug. The second constrains came from an UIStackView inside the view itself. I needed just to set stackView.translatesAutoresizingMaskIntoConstraints = false to solve the problem.