首页 > 解决方案 > 滚动视图通过打破约束工作

问题描述

我有一个UIViewController包含scrollViewscrollView被固定(0,0,0,0)到superview.

scrollView包含具有以下约束集内容视图:

在此处输入图像描述

在这个视图中,我放置了一个方形图像视图:它以这种方式固定,它的高度等于它的 superview 的宽度

在此处输入图像描述

到目前为止,我所描述的任何内容都是在 storyboard 中手动完成的。

然后,使用一个简单的按钮(所以,之后viewDidLoadviewDidLayoutSubviews等),我执行以下功能(375目前是硬编码的,但它是我用于测试目的的设备上heigth图像视图): frame

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 下scrollViewcontentView内创建 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 )>

问题是我不明白是什么限制导致了这个问题,最重要的是,为什么。你可以帮帮我吗?

标签: iosswiftuiscrollviewautolayoutconstraints

解决方案


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

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

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

有关更多详细信息,请遵循以下教程 如何使用自动布局配置 UIScrollView


推荐阅读