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

标签 ios swift uiscrollview tableview

我在我的 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/

相关文章:

iphone - Obj-C "@interface"的类文件?

ios - 向导航 Controller 添加另一个后退按钮

ios - Swift 将数据从 subview 发送到父 View Controller

ios - 如何在表格 View 中自动滚动? ( swift )

iphone - UIScrollView:单击将其滚动到顶部

ios - UIPageControl + 圆形(无限)滚动

ios - 为什么我的 "pull down to load data"卡在第 2 页而没有加载更多数据

iOS viewDidLoad 未被触发

ios - 如何在 iOS 中将视频作为背景?

Ios+Redlaser + 产品信息