我在我的 chid 视图控制器上设置了 4 个约束:
childController.view.translatesAutoresizingMaskIntoConstraints = false
heightConstraint = childController.view.heightAnchor.constraint(equalToConstant: 800.0)
leadingConstraint = childController.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
trailingConstraint = childController.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
topViewUpperConstraint = childController.view.topAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor, constant: -44)
topViewUpperConstraint.identifier = "topViewUpperConstraint"
NSLayoutConstraint.activate([heightConstraint, leadingConstraint, trailingConstraint, topViewUpperConstraint])
我的子视图控制器中有一个表视图,我希望它仅在我的子视图控制器topViewUpperContraint.constant
相对bottomLayoutGuide.topAnchor
于其superView
的 等于 -500时才可滚动。
我在一个UIScrollViewDelegate
方法中遍历子视图控制器的约束,以找到具有标识符的约束,"topViewUpperConstraint"
如下所示:
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
let viewConstraints = view.constraints
print("*** The number of constraint on the main view is: \(viewConstraints.count)")
for constraint in viewConstraints{
print("*** Constraint identifer is: \(constraint.identifier)")
if constraint.identifier == "topViewUpperConstraint" {
if constraint.constant == -500{
tableView.isScrollEnabled = true
} else {
tableView.isScrollEnabled = false
}
}
}
}
但是,"topViewUpperConstraint"
子视图控制器的约束集中的标识符没有任何约束。此外,主视图上设置了 13 个约束,而我只设置了 4 个。您能向我解释一下我在这里遗漏了什么吗?
当您激活约束时,它会被添加到约束中引用的两个视图的共同祖先的约束属性中。在您的示例中,您将视图的顶部约束到 bottomLayoutGuide 的顶部,因此该约束将被添加到视图层次结构中它们的共同祖先,这可能是您视图的超级视图。
有 13 个约束,因为Auto Layout正在为您看不到的事物设置约束,并且没有像bottomLayoutGuide那样明确添加。
如果你通过循环view.constraints
的ViewController
无添加你自己的任何限制
for constraint in view.constraints {
print("Item1: \(constraint.firstItem), Item2: \(constraint.secondItem)")
}
你会看到8
约束:
*** The number of constraint on the main view is: 8
Item1: <_UILayoutGuide: 0x7fcedcd07e20; frame = (0 0; 0 20); hidden = YES; layer = <CALayer: 0x608000025020>>, Item2: nil
Item1: <_UILayoutGuide: 0x7fcedcd07e20; frame = (0 0; 0 20); hidden = YES; layer = <CALayer: 0x608000025020>>, Item2: Optional(<UIView: 0x7fcedcd07a70; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x608000024fc0>>)
Item1: <_UILayoutGuide: 0x7fcedce05530; frame = (0 736; 0 0); hidden = YES; layer = <CALayer: 0x600000024ca0>>, Item2: nil
Item1: <_UILayoutGuide: 0x7fcedce05530; frame = (0 736; 0 0); hidden = YES; layer = <CALayer: 0x600000024ca0>>, Item2: Optional(<UIView: 0x7fcedcd07a70; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x608000024fc0>>)
Item1: <UIView: 0x7fcedcd07a70; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x608000024fc0>>, Item2: nil
Item1: <UIView: 0x7fcedcd07a70; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x608000024fc0>>, Item2: nil
Item1: <UIView: 0x7fcedcd07a70; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x608000024fc0>>, Item2: nil
Item1: <UIView: 0x7fcedcd07a70; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x608000024fc0>>, Item2: nil
其中包括view
自身的4 个约束和topLayoutGuide和bottomLayoutGuide各 2个约束。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句