在视图内发现未知约束

时间:2018-09-10 21:30:40

标签: ios swift constraints uipangesturerecognizer

我正在创建一个自定义类以使视图可扩展;该类与UIPanGestureRecognizer一起使用,该UIPanGestureRecognizer修改视图的高度约束。在调试代码时,我使用以下代码来显示与视图相关的高度限制。

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

奇怪的是,除了通过扩展视图的方法创建的成本约束之外,还有另外一个成本约束,我不知道从哪里来。这是控制台中打印的内容:

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

这是该课程的完整代码。

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()
    }

}

我将故事板中的视图类设置为“ ExpandableView”,并在超类viewController中对其进行了初始化,如下所示: 这是ViewController内部的代码,在该代码中我初始化了名为profileView的视图。

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

3 个答案:

答案 0 :(得分:0)

在视图上将translatesAutoresizingMaskIntoConstraints设置为false

当它为true时,它将创建一些约束来定义其位置和大小。

答案 1 :(得分:0)

发现了错误。第二个约束来自视图本身内部的UIStackView。我只需要设置stackView.translatesAutoresizingMaskIntoConstraints = false即可解决问题。

答案 2 :(得分:0)

您看到的意外约束是NSAutoresizingMaskLayoutConstraint的实例。之所以添加此项,是因为您已通过编程方式添加了一个组件(它似乎是UIStackView的一个实例),并且尚未将其translatesAutoResizingMaskIntoConstraints属性设置为false