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

时间:2018-08-24 04:00:29

标签: ios autolayout constraints anchor

我需要在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个约束。

有趣的是,它们的打印输出是不同的。使用 Autolayout Visual Format Language 打印一些无效的约束(。top和。left)。另外,请注意第三项中的(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)>]

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

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

这是因为UIScrollView要求以某种方式设置contentSize。通过将UIView的布局锚定到UIScrollView的侧面,自动布局仍然没有明确了解contentSizeUIScrollView的内容。

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

答案 2 :(得分:0)

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.

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

enter image description here

enter image description here