UIScrollView 中的 UILayoutPriority 不影响任何东西

UILayoutPriority in an UIScrollView doesn't affect anything

我有一个内容视图(在 xib 文件中定义),其中有一些标签和两个按钮(屏幕截图上的白色视图 1.a)。如果来了怎么显示在屏幕上,我来画方案;

ViewController View -> View (Custom View Class) -> UIScrollView -> ContentView (Xib file)

按钮与底部边距相关。他们都放在了底部。顶部按钮有一个约束,它有 10 个与最后一个标签底部相关的常量(这是 greater than equal constraint 并且 UILayoutPriority 为 5)。

问题:当我从按钮到标签给出约束时甚至大于等于,按钮仅在标签下方堆叠 10 点。即使底部有这么大的差距。 (截图2.a)

我想要实现的目标: 我想要如果所有内容都适合屏幕并且从最后一个标签到第一个按钮至少有 10 个约束,则不要滚动。如果没有,请滚动。

自定义视图 Class:

override init(frame: CGRect) {
    super.init(frame: frame)
    initialize()
}
func initialize() {
    self.translatesAutoresizingMaskIntoConstraints = false
    let name = String(describing: type(of: self))
    let nib = UINib(nibName: name, bundle: .main)
    nib.instantiate(withOwner: self, options: nil)

    cancelButton.layer.borderColor = UIColor.darkGray.cgColor
    cancelButton.layer.borderWidth = 1.0

    scrollView.translatesAutoresizingMaskIntoConstraints = false
    self.addSubview(self.scrollView)
    scrollView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
    scrollView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
    scrollView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
    scrollView.addSubview(contentView)

    self.contentView.translatesAutoresizingMaskIntoConstraints = false
    //self.contentView.clipsToBounds = true

    contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
    contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
    contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
    contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
    contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true 
}

示例截图:

如果内容适合视图,我想要如下图。如果按钮不能上升直到它适合,如果它不适合,视图应该是可滚动的。

截图2.a

下一行应该足以计算滚动视图中内容的高度,并为您提供您期望的正确行为。

contentView.heightAnchor.constraint(greaterThanOrEqualTo: scrollView.heightAnchor).isActive = true