gpt4 book ai didi

ios - 为什么没有记录约束标识符?

转载 作者:行者123 更新时间:2023-11-28 15:49:46 26 4
gpt4 key购买 nike

我在我的 chid View Controller 上设置了 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])

我的 subview Controller 中有一个 TableView ,我希望它仅在我的 subview Controller 的 topViewUpperContraint.constant 相对于 bottomLayoutGuide.topAnchor 时才可滚动它的 superView 等于 -500。

我在 UIScrollViewDelegate 方法中遍历 subview Controller 的约束,以找到具有标识符 "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
}
}
}
}

但是,在 subview Controller 的约束集中,标识符"topViewUpperConstraint" 没有任何约束。此外,主视图上设置了 13 个约束,而我只设置了 4 个。您能向我解释一下我在这里缺少什么吗?

enter image description here

最佳答案

当您激活一个约束时,它会被添加到约束中引用的两个 View 的共同祖先的constraints 属性中。在您的示例中,您将 View 的顶部约束到 bottomLayoutGuide 的顶部,因此约束将添加到它们在 View 层次结构中的共同祖先,这可能是您 View 的父 View 。

有 13 个约束,因为 Auto Layout 正在为您看不到的东西设置约束,并且没有像 bottomLayoutGuide 那样显式添加。


如果你循环遍历 ViewControllerview.constraints 而没有添加你自己的任何约束

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 个用于 view 本身的约束以及 2 个用于 topLayoutGuidebottomLayoutGuide 的约束。

关于ios - 为什么没有记录约束标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42468483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com