Scrollview 通过打破约束来工作

Scrollview working by breaking constraint

我有一个包含 scrollViewUIViewControllerscrollView 已固定 (0,0,0,0) superview.

scrollView 包含 Content View 并设置了以下约束:

在这个视图中,我放了一个正方形 image view:它是这样固定的,它的高度等于它的 superview 的宽度 .

到目前为止我所描述的任何内容都是在故事板中手动完成的

然后,使用一个简单的按钮(因此,在 viewDidLoadviewDidLayoutSubviews 等之后),我执行以下函数(375 是目前是硬编码的,但它是我用于测试目的的设备上 图像视图 frameheigth:

var startH = 0

for i in 0...10 {

let v = UIView(frame: CGRect(x: 0, y: 375+startH, width: 375, height: startH))
    v.backgroundColor = .random
    contentView.addSubview(v)
    startH += 100
}

let scrollerLayoutGuide = scrollView.contentLayoutGuide
scrollerLayoutGuide.heightAnchor.constraint(equalToConstant: CGFloat(startH+375)).isActive = true
scrollView.contentSize = contentView.frame.size

这应该(实际上它确实)在 imageView 下,在 contentView 中创建了 11 个视图滚动视图.

问题是当我执行此函数时出现以下错误:

[LayoutConstraints] Unable to simultaneously satisfy constraints.   
Probably at least one of the constraints in the following list is one you don't want.   
Try this:       (1) look at each constraint and try to figure out which you don't expect;       (2) find the code that added the unwanted constraint or constraints and fix it.  (
    "<NSLayoutConstraint:0x6000019d7d40 UIView:0x7fe66940aa30.height == UIScrollView:0x7fe66985b400.height   (active)>",
    "<NSLayoutConstraint:0x6000019d7d90 V:|-(0)-[UIView:0x7fe66940aa30]   (active, names: '|':UIScrollView:0x7fe66985b400 )>",
    "<NSLayoutConstraint:0x6000019d7de0 V:[UIView:0x7fe66940aa30]-(0)-|   (active, names: '|':UIScrollView:0x7fe66985b400 )>",
    "<NSLayoutConstraint:0x6000019e4690 UILayoutGuide:0x6000003c81c0'UIViewSafeAreaLayoutGuide'.bottom == UIScrollView:0x7fe66985b400.bottom   (active)>",
    "<NSLayoutConstraint:0x6000019e4730 UIScrollView:0x7fe66985b400.top == UILayoutGuide:0x6000003c81c0'UIViewSafeAreaLayoutGuide'.top   (active)>",
    "<NSLayoutConstraint:0x6000019e11d0 _UIScrollViewLayoutGuide:0x6000003ac1c0'UIScrollView-contentLayoutGuide'.height
== 1475   (active)>",
    "<NSLayoutConstraint:0x6000019e9540 'UIView-Encapsulated-Layout-Height' UIView:0x7fe66950bdb0.height == 667   (active)>",
    "<NSLayoutConstraint:0x6000019e45f0 'UIViewSafeAreaLayoutGuide-bottom' V:[UILayoutGuide:0x6000003c81c0'UIViewSafeAreaLayoutGuide']-(0)-|   (active, names: '|':UIView:0x7fe66950bdb0 )>",
    "<NSLayoutConstraint:0x6000019e4550 'UIViewSafeAreaLayoutGuide-top' V:|-(20)-[UILayoutGuide:0x6000003c81c0'UIViewSafeAreaLayoutGuide']   (active, names: '|':UIView:0x7fe66950bdb0 )>" )

Will attempt to recover by breaking constraint  <NSLayoutConstraint:0x6000019d7de0 V:[UIView:0x7fe66940aa30]-(0)-|   (active, names: '|':UIScrollView:0x7fe66985b400 )>

问题是我不明白是什么约束导致了这个问题,最重要的是,我不明白为什么。你能帮帮我吗?

您的约束与 contentView 之间存在冲突

1. There is Top, bottom, leading, trailing with the scrollview 2. Fixed height and fixed width constraint of contentView

这两个会相互冲突,因为 OS 不确定要满足哪个约束。 作为解决方案,降低高度和宽度 constraint 的优先级,以便 constraint 相对于 scrollView.

给予更多优先级

更多详情请参考以下教程 How to configure a UIScrollView with Auto Layout