简体   繁体   English

如何在滚动视图中使用自动布局?

[英]How do you use auto layout in a scroll view?

So I'm using a UIScrollView in a view controller in the latest Xcode. 所以我在最新的Xcode的视图控制器中使用UIScrollView I have buttons inside the scroll view. 我在滚动视图中有按钮。 When I run the application, the scroll view disappears. 当我运行该应用程序时,滚动视图消失了。 Can anyone tell me what is causing this and how the auto layout tools work? 谁能告诉我是什么原因造成的,以及自动布局工具如何工作?

Follow these steps, 跟着这些步骤, 在此处输入图片说明

  1. Take scroll view and set constraints (leading, trailing, top, bottom) with respect to the parent view. 采取滚动视图并相对于父视图设置约束(前导,尾随,顶部,底部)。 And set some background color to it so that it will visible at run time. 并为其设置一些背景色,以便在运行时可见。

  2. Take UIButton and place it over scrollview, and then set constraints for your button (leading, trailing, top) with respect to the scroll view, and also set the width and height constraints for button ,and also some background color to button so that it will visible at runtime. 取UIButton并将其放置在scrollview上,然后相对于滚动视图设置按钮的约束(前导,尾随,顶部),还设置按钮的宽度和高度约束,并为button设置一些背景色,以便它在运行时将可见。

Note: The hirearchy of your contents should be like this- -parentView -ScrollView -button 注意:您的内容的等级结构应如下所示--parentView -ScrollView -button

please let me know if anything is not clear. 请让我知道是否有任何不清楚的地方。

Thanks. 谢谢。

Here's a recipe for setting up a scroll view with Auto Layout. 这是使用“自动版式”设置滚动视图的方法。

  1. Drag out a scrollView and add it to your ViewController. 拖出一个scrollView并将其添加到您的ViewController中。 Pin its edges to the edges of its superView. 将其边缘固定到其superView的边缘。

  2. Drag out a new UIView and drop it into your scrollView. 拖出一个新的UIView并将其放入您的scrollView中。 This will serve as the contentView for the scrollView and it is the only top level view in the scrollView. 这将用作scrollView的contentView,并且是scrollView中唯一的顶层视图。 Pin all four of its edges to the edges of the scrollView. 将其所有四个边缘固定到scrollView的边缘。 Note: this will not set the content size of the scrollView. 注意:这不会设置scrollView的内容大小。

  3. If you want the scrollView to scroll vertically only, set the width of the contentView to the width of the scrollView. 如果只想使scrollView垂直滚动,请将contentView的宽度设置为scrollView的宽度。 To do this, go to the Document Outline View and control -drag from the contentView to the scrollView and select Equal Widths from the pop up. 为此,请转到“ 文档大纲视图”控制 -从contentView拖动到scrollView,然后从弹出窗口中选择“ 等宽 ”。

  4. To set the height of the content view, you can set a height constraint and then change its height to the value you want. 要设置内容视图的高度,可以设置高度约束,然后将其高度更改为所需的值。 The value needs to be larger than the height of the scrollView if you want it to scroll. 如果要滚动,则该值必须大于scrollView的高度。 The other way to do it is to make sure that all of the items in your contentView all have height constraints and are all pinned to each other, and the topmost one is pinned to the top of the contentView and the bottom one is pinned to the bottom of the contentView. 另一种方法是确保contentView中的所有项目都具有高度限制,并且彼此固定,并且最上面的一个固定在contentView的顶部,而最下面的固定在contentView的顶部。 contentView的底部。 If you do this, then Auto Layout will be able to compute the height of the contentView for you. 如果执行此操作,则“自动布局”将能够为您计算contentView的高度。 If this total height is greater than the height of the scrollView, then your contentView will scroll. 如果此总高度大于scrollView的高度,则contentView将滚动。

  5. Finally, add in your button. 最后,添加您的按钮。 You can now center the button horizontally in the contentView since you established the width of the contentView in step 3. 现在,由于您在步骤3中确定了contentView的宽度,因此您现在可以将按钮水平放置在contentView中。

Hope this will help others as well 希望这也会对其他人有帮助

  1. When you working scrollView with autoLayout, the view hierarchy should be like 在使用autoLayout使用scrollView时,视图层次结构应类似于

在此处输入图片说明

A view( View1 ) which contain the scrollView and a view( View2 ) which contain inside the scrollView. 一个包含scrollView的视图( View1 )和一个包含scrollView内部的视图( View2 )。

Now put the Constraint for View1 现在将View1的约束

在此处输入图片说明

yes, pinned it from all the side. 是的,从各个角度固定它。

Do the same step for scrollView as well as View2 , Now it will be giving warning like scrollable content size Ambiguity 做了滚动相同的步骤,以及视图2,现在将给予像滚动的内容大小模糊警告

Now,the most important step is to press CTRL and drag from the View2 to View1 , scroll down the popover and select equal width 现在,最重要的 步骤是按CTRL键并将其从View2拖动到View1 ,向下滚动弹出窗口并选择相等的宽度

在此处输入图片说明

Now it will give you the warning for ambitious scrollView height but when you give proper constraints to the inner element(inside the View1 ) it will also go away. 现在,它将向您发出警告,提醒您滚动显示的高度过高,但是当您对内部元素(在View1内 )赋予适当的约束时 ,它也会消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM