我在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个。你能解释一下我在这里缺少什么吗?
答案 0 :(得分:1)
激活约束时,会将其添加到约束中引用的两个视图的公共祖先的 constraints 属性中。在您的示例中,您将视图的顶部约束到bottomLayoutGuide的顶部,因此约束将添加到视图层次结构中的共同祖先,这可能是您视图的超级视图。
有13个限制条件,因为自动布局正在为您不会看到的内容设置约束,并且没有像 bottomLayoutGuide 那样明确添加。< / p>
如果您在OS
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
本身的4个约束, topLayoutGuide 和 bottomLayoutGuide 各2个。