在视图中发现未知约束
Unknow constrain found inside a view
我正在创建自定义 class 以使视图可扩展; class 与修改视图高度限制的 UIPanGestureRecognizer 一起使用。在调试代码时,我使用以下代码来显示与视图关联的高度限制。
print(self.constraints.filter({ [=10=].firstAttribute == .height}).map{print("costraints \([=10=])")})
奇怪的是,除了扩展视图的方法创建的约束之外,还有一个我不知道从哪里来的约束。这是控制台中打印的内容:
constraint <NSAutoresizingMaskLayoutConstraint:0x6140002828f0 h=--& v=--& UIStackView:0x7ff1c871d0e0.height == 190 (active)>
constraint <NSLayoutConstraint:0x60800009c160 PhotoMapping.ExpandableView:0x7ff1c8416fa0.height == 100 (active)>
这是 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({ [=12=].firstAttribute == .height}).isEmpty {
return self.constraints.filter({[=12=].firstAttribute == .height && [=12=].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({ [=12=].firstAttribute == .height}).isEmpty {
self.constraints.filter({[=12=].firstAttribute == .height && [=12=].secondItem == nil}).first?.constant = newConstant
self.constraints.filter({[=12=].firstAttribute == .height && [=12=].secondItem == nil}).map{print("constant \([=12=].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 中将视图的 class 设置为 "ExpandableView" 并在 superclass viewController 中对其进行了初始化,如下所示:
这是 ViewController 中的代码,我在其中初始化名为 profileView 的视图。
profileView.isUserInteractionEnabled = true
profileView.initialize(minHeight: 100, maxHeight: 190)
将您的视图 translatesAutoresizingMaskIntoConstraints
设置为 false
。
当它是 true
时,它将创建一些定义其位置和大小的约束。
发现错误。第二个约束来自视图本身内部的 UIStackView。我只需要设置 stackView.translatesAutoresizingMaskIntoConstraints = false
就可以解决问题。
您看到的意外约束是 NSAutoresizingMaskLayoutConstraint
的一个实例。这是因为您以编程方式添加了一个组件(它似乎是 UIStackView
的一个实例)并且您没有将其 translatesAutoResizingMaskIntoConstraints
属性 设置为 false
)
我正在创建自定义 class 以使视图可扩展; class 与修改视图高度限制的 UIPanGestureRecognizer 一起使用。在调试代码时,我使用以下代码来显示与视图关联的高度限制。
print(self.constraints.filter({ [=10=].firstAttribute == .height}).map{print("costraints \([=10=])")})
奇怪的是,除了扩展视图的方法创建的约束之外,还有一个我不知道从哪里来的约束。这是控制台中打印的内容:
constraint <NSAutoresizingMaskLayoutConstraint:0x6140002828f0 h=--& v=--& UIStackView:0x7ff1c871d0e0.height == 190 (active)>
constraint <NSLayoutConstraint:0x60800009c160 PhotoMapping.ExpandableView:0x7ff1c8416fa0.height == 100 (active)>
这是 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({ [=12=].firstAttribute == .height}).isEmpty {
return self.constraints.filter({[=12=].firstAttribute == .height && [=12=].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({ [=12=].firstAttribute == .height}).isEmpty {
self.constraints.filter({[=12=].firstAttribute == .height && [=12=].secondItem == nil}).first?.constant = newConstant
self.constraints.filter({[=12=].firstAttribute == .height && [=12=].secondItem == nil}).map{print("constant \([=12=].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 中将视图的 class 设置为 "ExpandableView" 并在 superclass viewController 中对其进行了初始化,如下所示: 这是 ViewController 中的代码,我在其中初始化名为 profileView 的视图。
profileView.isUserInteractionEnabled = true
profileView.initialize(minHeight: 100, maxHeight: 190)
将您的视图 translatesAutoresizingMaskIntoConstraints
设置为 false
。
当它是 true
时,它将创建一些定义其位置和大小的约束。
发现错误。第二个约束来自视图本身内部的 UIStackView。我只需要设置 stackView.translatesAutoresizingMaskIntoConstraints = false
就可以解决问题。
您看到的意外约束是 NSAutoresizingMaskLayoutConstraint
的一个实例。这是因为您以编程方式添加了一个组件(它似乎是 UIStackView
的一个实例)并且您没有将其 translatesAutoResizingMaskIntoConstraints
属性 设置为 false
)