UIScrollView 中的 UIView 遵守某些约束但不遵守其他约束

UIView in UIScrollView respects some constraints but not other

我需要在UIScrollView中添加一个containerView,然后在containerView中添加多个子视图。出于某种原因,containerView 不遵守 top/bottom/left/rightAnchor 约束,但它适用于 width/height/centerX/centerYAnchor

注意: 如果父视图是 UIView 而不是 UIScrollView,它工作正常。

该项目是 100% 基于代码的。使用 Swift 4.1 和 Xcode 9.4

这不行

containerView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
containerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0).isActive = true
containerView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: 0).isActive = true
containerView.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: 0).isActive = true

这个有效

containerView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
containerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true
containerView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor).isActive = true

在这两种情况下,scrollView.constraints 数组总共包含 4 个约束。

有趣的是它们的打印输出是不同的。一些不起作用的约束(.top 和 .left)是使用 Autolayout Visual Format Language 打印的。另外,请注意第三个中的 (LTR):

ScrollView [
<NSLayoutConstraint:V:|-(0)-[UIView] (active, names: '|':UIScrollView:)>,
<NSLayoutConstraint:UIView.bottom == UIScrollView.bottom (active)>,
<NSLayoutConstraint:H:|-(0)-[UIView](LTR) (active, names: '|':UIScrollView:)>,
<NSLayoutConstraint:UIView.right == UIScrollView.right (active)>]

有效的约束打印如下:

ScrollView [
<NSLayoutConstraint:UIView.width == UIScrollView.width (active)>,
<NSLayoutConstraint:UIView.height == UIScrollView.height (active)>, 
<NSLayoutConstraint:UIView.centerX == UIScrollView.centerX (active)>, 
<NSLayoutConstraint:UIView.centerY == UIScrollView.centerY (active)>]

我研究了 Whosebug 并发现了几个问题,例如 ,但它们并没有真正帮助我解释问题所在(或约束的 UIScrollView 要求)。

有什么想法吗?

Go through the following points in order to use scrollview in your application. 
 1. First add UIScrollview and give it constrain in view(left, right,width,height).[![enter image description here][1]][1]
 2. Now each scrollview has content view which should be there , we cannot add our required views directly to UIScrollview.
 3. Add view to scrollview(we name it content view) , give it top,bottom, left and right constrain. Apart from these we need to add height and width constrain to the content view. 
 4. If you want to have vertical scrollview then give width equal to scrollview and a proper height (like height constrain = 600)or greater than scrollview height.
 5. If you want to have horizontal scrollview then give height equal to scrollview and width greater than actual width of scrollview.

看看下面添加的内容视图的约束

这是因为 UIScrollView 需要以某种方式设置 contentSize。通过将 UIView 的布局锚定到 UIScrollView 的两侧,自动布局仍然没有明确了解 UIScrollViewcontentSize 是什么。

由于 UIScrollView 可能锚定到某个父视图,因此 UIScrollView 的高度和宽度已经定义。通过给 UIView 这些约束,自动布局可以确定 UIView 的大小,然后使用该大小设置 UIScrollView.

contentSize

UIScrollView 需要滚动其中的一些内容。您正在添加的视图(在滚动视图内)没有大小(高度和宽度),因此滚动视图无法识别其内容的大小。

为视图添加大小(在滚动视图内),它将起作用。

containerView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
containerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0).isActive = true
containerView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: 0).isActive = true
containerView.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: 0).isActive = true
// Size constraints
containerView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
containerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true

// To check scrolling of container view; try this
containerView.widthAnchor.constraint(equalTo: scrollView.widthAnchor + 50.0).isActive = true
containerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor + 50.0).isActive = true