【问题标题】:Scrollview working by breaking constraint滚动视图通过打破约束工作
【发布时间】:2019-07-15 05:12:37
【问题描述】:

我有一个包含 scrollViewUIViewControllerscrollView 被固定 (0,0,0,0) 到 superview

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

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

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

然后,使用一个简单的按钮(所以,在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 )>

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

【问题讨论】:

  • 降低内容视图的高宽限制的优先级
  • 我能做到,但为什么呢?
  • 您的内容视图对滚动视图具有上下前导和尾随约束。如果你给它高度和宽度约束,操作系统也不确定要遵循哪一个,内容视图是固定高度还是相对于滚动视图
  • 好的,只需将 content.height 约束设置为 height @250 即可正常工作。谢谢!您能否发表您的评论作为答案?

标签: ios swift uiscrollview autolayout constraints


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多