ios - 滚动视图通过打破约束工作
问题描述
我有一个UIViewController
包含scrollView:scrollView被固定(0,0,0,0)到superview
.
scrollView包含具有以下约束集的内容视图:
在这个视图中,我放置了一个方形图像视图:它以这种方式固定,它的高度等于它的 superview 的宽度。
到目前为止,我所描述的任何内容都是在 storyboard 中手动完成的。
然后,使用一个简单的按钮(所以,之后viewDidLoad
,viewDidLayoutSubviews
等),我执行以下功能(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 下,在scrollView的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
这两者会相互冲突,因为操作系统不确定要满足哪个约束。作为一种解决方案,降低高度和宽度的优先级,constraint
以便constraint
相对于scrollView
.
有关更多详细信息,请遵循以下教程 如何使用自动布局配置 UIScrollView
推荐阅读
- asp.net - 错误“没有'Access-Control-Allow-Origin'”,但cors允许一切
- python-3.x - PyQt5 中从“a”到 Qt.Key_A
- hadoop-yarn - 在 Yarn 集群上提交 Flink 作业以从上一个保存点恢复其状态
- javascript - jQuery表单:提交验证问题+提交后留在同一页面
- c# - 时间:2019-05-10 标签:c#remove(null) from XML tags
- php - Laravel 5.6 登录后重定向到登录页面
- java - maven:无法运行 junit5-jupiter-starter-maven?
- moodle - 安装moodle时的php_intl.dll问题
- angular - Angular + Chrome - 同一个应用程序的两个选项卡冻结了两个 HTTP 查询
- html - 如何让 box-shadow 左右只在不同元素上无缝显示