答案 0 :(得分:3)
使用此代码(Swift 4+和Xcode 9.4.1)
import UIKit
class ViewController: UIViewController {
@IBOutlet var subView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
print(self.view.getAllConstraints().count)
print(self.subView.getAllConstraints().count)
}
}
extension UIView {
func getAllConstraints() -> [NSLayoutConstraint] {
var views = [self]
var view = self
while let superview = view.superview {
views.append(superview)
view = superview
}
return views.flatMap({ $0.constraints }).filter { constraint in
return constraint.firstItem as? UIView == self ||
constraint.secondItem as? UIView == self
}
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
只需尝试在viewDidAppear
中打印约束。在viewDidLoad
方法约束中,视图未加载,因此,只需在viewDidAppear
方法中尝试。