ios - View 内发现未知约束

标签 ios swift constraints uipangesturerecognizer

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

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

奇怪的是,除了通过扩展 View 的方法创建的余弦之外,还有一个不知从何而来的余弦。以下是控制台中打印的内容:

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

}

我在 StoryBoard 中将 View 的类设置为“ExpandableView”,并在父类(super class) viewController 中初始化它,如下所示: 这是 ViewController 内的代码,我在其中初始化名为 profileView 的 View 。

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

最佳答案

将 View 上的 translatesAutoresizingMaskIntoConstraints 设置为 false

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

关于ios - View 内发现未知约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52265746/

相关文章:

ios - 手动设置 UILabel 的高度并自动布局

ios - 使用约束重新定义协议(protocol)功能,而无需公开方法

ios - 如何刷新 UITableViewCell 中的数据?

ios - 核心数据最佳实践——我在利用 C.D.正确有效? - iOS swift

ios - 如何检查 AVAudioPlayer 是否仍在运行?

arrays - 在 Postgresql 中为文本数组设置检查约束

gcc - gcc 内联汇编的 volatile 与编译器障碍

ios - UITableView tableHeaderView 中的 UIButton 不响应触摸

ios - 无法将自定义字体添加到 Xcode

string - 如何在 Apple Swift 中定义一个新的 "String"类型?