ios 如何使用 Autolayout 在我的 UIScrollview 上设置约束?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20223021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use Autolayout to set constraints on my UIScrollview?
提问by user1459524
I have spent two days trying out the various solutions for Mixed and Pure Autolayout approachesto achieve what was a trivial scrollview setup prior to autolayout, and it's now official - I must be too stupid. I am setting this up mostly in Storyboard (well, it's just the way it is).
我花了两天时间尝试混合和纯自动布局方法的各种解决方案,以实现自动布局之前的微不足道的滚动视图设置,现在它是正式的 - 我一定是太愚蠢了。我主要在 Storyboard 中设置它(嗯,它就是这样)。
So here's my plea for help.
所以这是我的请求帮助。
Viewtree:
视图树:
UIView
-UIView
-UIView
..-UIScrollview
...-UIButton
...-UIButton
...-UIButton
The buttons are supposed to scroll horizontally (left to right and vice versa). Can someone pleaselet me know how to set the constraints to achieve this using pure Autolayout???
按钮应该水平滚动(从左到右,反之亦然)。有人可以请让我知道如何设置的约束来实现这个使用纯自动布局???
--
——
I have tried the mixed approach, like so:
我尝试了混合方法,如下所示:
UIView
- UIView
- UIView
..-UIScrollview
...-UIView (contentview)
....-UIButton
....-UIButton
....-UIButton
...and setting fixed width and height constraints for the contentview
and the translatesAutoresizingMaskIntoConstraints
settings as per Apple's TechNote. The buttons and scrollview are set up using constraints. This gets the scrollview scrolling (yay) but alas, it scrolls too far! As far as I can tell, the scroll width is somehow doubled from what I set the contentview at???!!!???
......对于与设置固定宽度和高度的限制contentview
以及translatesAutoresizingMaskIntoConstraints
按照苹果公司的技术说明设定。按钮和滚动视图是使用约束设置的。这使滚动视图滚动(是的),但是,它滚动得太远了!据我所知,滚动宽度以某种方式从我设置的内容视图中翻了一番???!!!???
I tried the pure autolayout approach as well, both with contentview
and without. Allthe views are translatesAutoresizingMaskIntoConstraints=NO
, except for self.view
. The buttons have fixed width/height constraints, and are pinned to all four edges of the scrollview. Nothing scrolls.
我也尝试了纯自动布局方法,无论有contentview
没有。所有的意见都是translatesAutoresizingMaskIntoConstraints=NO
,除了self.view
。按钮具有固定的宽度/高度约束,并固定到滚动视图的所有四个边缘。什么都不会滚动。
So I am totally baffled why I can't get it to work correctly. Any help is much appreciated, and if you need any other info, please ask!
所以我完全困惑为什么我不能让它正常工作。非常感谢任何帮助,如果您需要任何其他信息,请询问!
UPDATED Screenshot with solution -buttonZ constraints:
带有解决方案的更新屏幕截图 -buttonZ 约束:
EDIT @ Jamie ForrestSo the solution turns out to be the wrong trailing constraint on the last button. Instead of 6441, the value I had set was negative, -6441. The tricky thing is, that when setting the value in storyboard, there are two options in the Pin toolbar:
编辑@ Jamie Forrest所以解决方案是最后一个按钮上的错误尾随约束。我设置的值不是 6441,而是负值 -6441。棘手的是,在故事板中设置值时,Pin 工具栏中有两个选项:
The Current Canvas Value is negative (leading to no scroll), and the option below is positive (activating scroll). This means I'm not stupid but at least half-blind I guess. Although, to my defense, isn't it somewhat disturbing that XCode doesn't show an error for the "incorrect" setting?
当前画布值为负(导致无滚动),下面的选项为正(激活滚动)。这意味着我并不愚蠢,但我猜至少是半盲。虽然,为了我的辩护,XCode 没有显示“不正确”设置的错误是不是有点令人不安?
EDITED AGAINNow this is funny... changing the trailing value from -6441 (no scroll) to 6441 enabled scroll. But my old friend the "too much contentsize" was back, leading to a content size twice as large as what it should be! The solution to get the correct content scroll was to set the trailing constraint to ZERO! This is not obvious when working in Storyboard but looking at @Infinity James' code, it is what it should be.
再次编辑现在这很有趣……将尾随值从 -6441(无滚动)更改为 6441 启用滚动。但是我的老朋友“内容大小太多”又回来了,导致内容大小是应有的大小的两倍!获得正确内容滚动的解决方案是将尾随约束设置为零!这在 Storyboard 中工作时并不明显,但查看@Infinity James 的代码,它应该是这样。
采纳答案by Jamie Forrest
It's hard to see the exact values and setup of your constraints as you've pasted them here, so I'm not sure from looking at your screenshots where you have gone wrong.
当您将它们粘贴到此处时,很难看到约束的确切值和设置,因此我不确定从查看您的屏幕截图中您哪里出错了。
In lieu of an explanation of what's wrong in your setup, I've created a basic sample projectwith a very similar view hierarchy and constraint setup to the one you describe. The horizontal scrolling works as expected in the sample project, which uses the "Pure AutoLayout" approach that Apple describes in the Technical Note.
我创建了一个基本示例项目,其视图层次结构和约束设置与您所描述的非常相似,而不是解释您的设置出了什么问题。水平滚动在示例项目中按预期工作,该项目使用 Apple 在Technical Note 中描述的“Pure AutoLayout”方法。
I also had a lot of trouble originally getting Auto Layout to work with UIScrollView
. The key to getting it to work is making sure that all of the items in the scroll view, taken together, have constraints that eventually link to all sides of the scroll view and that contribute to the AutoLayout system being able to determine a contentSize for the scroll view that will be bigger than its frame. It looks like you were trying to do that in your code, but maybe you had some superfluous constraints in there that were making the contentSize too small.
最初使用 Auto Layout 时我也遇到了很多麻烦UIScrollView
。让它工作的关键是确保滚动视图中的所有项目,放在一起,有约束,最终链接到滚动视图的所有边,并有助于 AutoLayout 系统能够确定一个 contentSize滚动视图将大于其框架。看起来您正试图在代码中执行此操作,但也许您在其中设置了一些多余的约束,导致 contentSize 太小。
Also of note, as others mentioned, with AutoLayout and UIScrollview, you no longer set the contentSize explicitly. The AutoLayout System calculates the contentSize based on your constraints.
另外值得注意的是,正如其他人提到的,使用 AutoLayout 和 UIScrollview,您不再显式设置 contentSize。AutoLayout 系统根据您的约束计算 contentSize。
I also found this ebook chapterto be very helpful in making me understand how all this works. Hope all this helps.
我还发现这本电子书章节对让我了解所有这些是如何工作的非常有帮助。希望这一切都有帮助。
回答by backslash-f
LOL welcome to the stupidity club. I'm one of the founders. :D
LOL 欢迎来到愚蠢俱乐部。我是创始人之一。:D
For VERTICAL scrolling: the only way I could get it to work (iOS 8, Xcode 6 and pure autolayout) was adding the following constraints to my Scroll View (all related to the superview):
对于垂直滚动:我可以让它工作的唯一方法(iOS 8、Xcode 6 和纯自动布局)是向我的滚动视图添加以下约束(都与超级视图相关):
- Equal Widths
- Equal Heights
- Center Y Alignment
- Center X Alignment
- 等宽
- 等高
- 中心 Y 对齐
- 中心 X 对齐
My structure:
我的结构:
UIView
- ScrollView
- Subview
- Subview
- Subview
- Subview
- ...
This is the final result:
这是最终结果:
This is the setup:
这是设置:
这是项目。
Hopefully this would save someone from GOING TO SLEEP AT 5 AM. :D
希望这可以避免有人在凌晨 5 点睡觉。:D
回答by Suragch
Simple Self-Contained Example
简单的自包含示例
Judging by the high number of votes on the question and the low number of votes on the answers, people are not finding an understandable and quick solution here. Let me try to add one. This project is a self-contained example done completely in the Interface Builder. You should be able to work through it in 10 minutes or less. Then you can apply the concepts you learned to your own project.
从这个问题的高票数和答案的低票数来看,人们在这里找不到可以理解和快速的解决方案。让我尝试添加一个。这个项目是一个完全在 Interface Builder 中完成的独立示例。您应该能够在 10 分钟或更短的时间内完成它。然后,您可以将学到的概念应用到您自己的项目中。
The original question asks about scrolling buttons. Here I just use UIView
s but they can represent whatever view you like. I also chose horizontal scrolling because the storyboard screenshots are more compact for this format. The principles are the same for vertical scrolling, though.
原始问题询问滚动按钮。这里我只使用UIView
s 但它们可以代表您喜欢的任何视图。我还选择了水平滚动,因为这种格式的故事板截图更紧凑。不过,垂直滚动的原理是相同的。
Key concepts
关键概念
- The
UIScrollView
should only use one subview. This is a 'UIView' that serves as content view to hold everything you wish to scroll. - Make the content view and the scroll view's parenthave equal heights for horizontal scrolling. (Equal widths for vertical scrolling)
- Make sure that all of the scrollable content has a set width and is pinned on all sides.
- 本
UIScrollView
应该只使用一个子视图。这是一个“UIView”,用作内容视图来保存您想要滚动的所有内容。 - 使内容视图和滚动视图的父级具有相同的水平滚动高度。(垂直滚动等宽)
- 确保所有可滚动内容都有固定的宽度并固定在所有侧面。
Start a new project
开始一个新项目
It can be just a single view application.
它可以只是一个单一的视图应用程序。
Storyboard
故事板
In this example we will make a horizontal scroll view. Select the View Controller and then choose Freeform in the Size Inspector. Make the width 1,000
and the height 300
. This just gives us room on the storyboard to add content that will scroll.
在这个例子中,我们将制作一个水平滚动视图。选择 View Controller,然后在 Size Inspector 中选择 Freeform。制作宽度1,000
和高度300
。这只是给我们在情节提要上添加将滚动的内容的空间。
Add a Scroll View
添加滚动视图
Add a UIScrollView
and pin all four sides to the root view of the view controller.
添加 aUIScrollView
并将所有四个边固定到视图控制器的根视图。
Add a Content View
添加内容视图
Add a UIView
as a subview to the scroll view. This is key.Don't try to add lots of subviews to the scroll view. Just add a single UIView
. This will be your content view for the other views you want to scroll. Pin the content view to the scroll view on all four sides.
将 aUIView
作为子视图添加到滚动视图。这是关键。不要尝试向滚动视图添加大量子视图。只需添加一个UIView
. 这将是您要滚动的其他视图的内容视图。将内容视图固定到所有四个侧面的滚动视图。
Equal Heights
等高
Now in the Document Outline, Commandclick both the content view and the scroll view's parent viewin order to select them both. Then set the heights to be equal (Controldrag from the Content View to the Scroll View). This is also key.Because we are scrolling horizontally, the scroll view's content view won't know how high it should be unless we set it in this way.
现在在文档大纲中,Command单击内容视图和滚动视图的父视图以将它们都选中。然后将高度设置为相等(Control从内容视图拖动到滚动视图)。这也是关键。因为我们是水平滚动的,滚动视图的内容视图不会知道它应该有多高,除非我们这样设置。
Note:
笔记:
- If we were making the content scroll vertically, then we would set the content view's width to be equal to the scroll view's parent's width.
- 如果我们让内容垂直滚动,那么我们会将内容视图的宽度设置为等于滚动视图的父级宽度。
Add content
添加内容
Add three UIView
s and give them all constraints. I used 8 point margins for everything.
添加三个UIView
s 并给它们所有约束。我对所有内容都使用了 8 点边距。
Constraints:
约束:
- Green view: pin the top, left, and bottom edges. Make the width 400.
- Red view: pin the top, left, and bottom edges. Make the width 300.
- Purple view: pin all four edges edges. Make the width whatever the remaining space is (268 in this case).
- 绿色视图:固定顶部、左侧和底部边缘。使宽度为 400。
- 红色视图:固定顶部、左侧和底部边缘。使宽度为 300。
- 紫色视图:固定所有四个边缘。使宽度与剩余空间无关(在本例中为 268)。
Setting the width constraints is also keyso that the scroll view knows how wide its content view will be.
设置宽度约束也是关键,以便滚动视图知道其内容视图的宽度。
Finished
完成的
That's all. You can run your project now. It should behave like the scrolling image at the top of this answer.
就这样。您现在可以运行您的项目。它的行为应该像这个答案顶部的滚动图像。
For vertical scrolling, just swap all the width and height directions in this example (tested and working).
对于垂直滚动,只需交换本示例中的所有宽度和高度方向(已测试和工作)。
Further Study
进一步研究
回答by Infinity James
The contentSize is implicitly set by applying the constraints inside of the UIScrollView.
contentSize 是通过在 UIScrollView 内部应用约束来隐式设置的。
For example, is you have a UIScrollView inside of a UIView it will look like this (as I am sure you are aware):
例如,您是否在 UIView 中有一个 UIScrollView 它看起来像这样(我相信您知道):
UIView *containerView = [[UIView alloc] init];
UIScrollView *scrollView = [[UIScrollView alloc] init];
[containerView addSubview:scrollView];
containerView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(containerView, scrollView);
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|"
options:kNilOptions
metrics:nil
views:viewsDictionary]];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|"
options:kNilOptions
metrics:nil
That will set the scrollView to fill the size of the containerView (so the containerView will have to be of a certain size).
这将设置 scrollView 以填充 containerView 的大小(因此 containerView 必须具有特定大小)。
You can then adjust the contentSize of the UIScrollView by implicitly setting it to be large enough to hold the buttons like this:
然后,您可以通过隐式设置 UIScrollView 的 contentSize 来调整它的 contentSize 以使其足够大以容纳这样的按钮:
UIButton *buttonA = [[UIButton alloc] init];
UIButton *buttonB = [[UIButton alloc] init];
UIButton *buttonC = [[UIButton alloc] init];
[scrollView addSubview:buttonA];
[scrollView addSubview:buttonB];
[scrollView addSubview:buttonC];
buttonA.translatesAutoresizingMaskIntoConstraints = NO;
buttonB.translatesAutoresizingMaskIntoConstraints = NO;
buttonC.translatesAutoresizingMaskIntoConstraints = NO;
viewsDictionary = NSDictionaryOfVariableBindings(scrollView, buttonA, buttonB, buttonC);
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[buttonA]-|"
options:kNilOptions
metrics:nil
views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[buttonA]-[buttonB]-[buttonC]-|"
options:NSLayoutFormatAlignAllBaseline
metrics:nil
views:viewsDictionary]];
回答by Danyun Liu
There are so many questions about using AutoLayout with UIScrollView, the key point which we ignore is that the inner views of the UIScrollView make constraints against the Content Viewbut not the UIScrollView itself. Refer to the Technical Note TN2154, you can find:
UIScrollView 使用 AutoLayout 有很多问题,我们忽略的关键点是 UIScrollView 的内部视图对Content View进行了约束,而不是 UIScrollView 本身。请参阅技术说明 TN2154,您可以找到:
The UIScrollView class scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the top, left, bottom, and right edges within a scroll view now mean the edges of its content view.
UIScrollView 类通过更改其边界的原点来滚动其内容。为了使其与自动布局一起工作,滚动视图中的顶部、左侧、底部和右侧边缘现在表示其内容视图的边缘。
The following figure will depicts that:
下图将说明:
You can find the trailing space is 500 points, if the constraint is made to the UIScrollView, the view will be miss placed and should be update its frame. However, no warnings and no errors. Because all the constraints are against the content view.
你可以发现拖尾空间是 500 点,如果对 UIScrollView 进行约束,视图将被错过,应该更新它的框架。但是,没有警告也没有错误。因为所有的约束都是针对内容视图的。
UIScrollView will calculate the size of the content view according to the constraints of the inner views. (For the example, the content size: width = 100(leading space) + 200 (view's width) + 500 (trailing space), height = 131 (top spacing) + 200(height) + 269(bottom spacing)
UIScrollView 会根据内部视图的约束来计算内容视图的大小。(例如,内容大小:宽度= 100(前导空间)+ 200(视图宽度)+ 500(尾随空间),高度= 131(顶部间距)+ 200(高度)+ 269(底部间距)
How to add constraints for views in the UIScrollView:
如何在 UIScrollView 中为视图添加约束:
- Imaging the positions of views in the content view.
- Add top, right, bottom, left spacing to the edges of the content view, in addition, also the width and height of these views.
- 对内容视图中的视图位置进行成像。
- 将顶部、右侧、底部、左侧间距添加到内容视图的边缘,此外,还添加这些视图的宽度和高度。
And all it is done.
而这一切都完成了。
An easy way to deal with AutoLayout with scrollview is to add a container view containing all subviews in the scroll view.
使用滚动视图处理 AutoLayout 的一种简单方法是添加一个包含滚动视图中所有子视图的容器视图。
Conclusion: the key point to understand AutoLayout with UIScrollView is inner views make constraints against the content view but not UIScrollView itself.
结论:使用 UIScrollView 理解 AutoLayout 的关键是内部视图对内容视图而不是 UIScrollView 本身进行约束。
attached example code
附上示例代码
回答by zeeawan
The following solution worked for me for scrollView with autolayout and without contentSize:
以下解决方案适用于带有 autolayout 且没有 contentSize 的 scrollView:
- Drag n drop a scrollView to viewController and apply whatever constraints to cover the space you want.
- Drag n drop a UIViewinside the scrollView and make it cover the whole space of scrollView and apply constraints to be top, left, right, bottom spacefrom scrollView.
- Set the height(and width if horizontal scrolling is required) of the inner view as per the need of scrolling. This part can also be done from code if required.
- Critical. After you set the height to some large value in point (3), go back to point (2) and be certain to set the top, left, right, bottom values back to zeroas Xcode may have changed them for you when you force changed the height in (3).
- 将 scrollView 拖放到 viewController 并应用任何约束来覆盖您想要的空间。
- 在 scrollView 中拖放一个UIView并使其覆盖 scrollView 的整个空间,并将约束应用到scrollView 的顶部、左侧、右侧、底部空间。
- 根据滚动需要设置内部视图的高度(如果需要水平滚动,则设置宽度)。如果需要,这部分也可以从代码中完成。
- 关键。在点 (3) 中将高度设置为某个较大的值后,返回点 (2) 并确保将顶部、左侧、右侧、底部的值设置回零,因为当您强制执行时,Xcode 可能已经为您更改了它们更改了 (3) 中的高度。
And you're done. Now, you can add any number of controls on this view and apply the constraints relevant to each other (which don't seem working without this view). If you don't want to use this view then you'll have to apply constraints for each control related to scrollView (not related to each other).
你已经完成了。现在,您可以在此视图上添加任意数量的控件并应用彼此相关的约束(如果没有此视图,这些约束似乎不起作用)。如果您不想使用此视图,则必须为与 scrollView 相关的每个控件(彼此不相关)应用约束。
The overwhelming tip..............
压倒性的提示…………
Critical. Let's say for clarity the UIScrollView is 1000 wideand 100 high. (In fact normally these values would be dynamic, of course, depending on the width of the device etc. But for now just say 1000 wide and 100 high.) Let's say you are doing a horizontal scroll. So put a UIView inside the UIScrollView. (That is the "content view".) Set all four constraints of the content view top, bottom, leading, trailing, to the scroll view. Make them all zero even if that seems wrong.Set the height of the content UIView to 100 and forget about that. Now: you want to scroll horizontally, so set the width of the content view to be let's say 1225.
关键。为了清楚起见,让我们说 UIScrollView 是1000 宽和 100 高。(实际上,这些值通常是动态的,当然,取决于设备的宽度等。但现在只说 1000 宽和 100 高。)假设您正在做一个水平滚动。所以在 UIScrollView 里面放一个 UIView。(即“内容视图”。)将内容视图顶部、底部、前导、尾随的所有四个约束设置为滚动视图。即使这看起来是错误的,也将它们全部设为零。将内容 UIView 的高度设置为 100 并忘记它。现在:您想水平滚动,因此将内容视图的宽度设置为 1225。
Note that the width of the content view is now 225 bigger than the width of the parent scroll view. That's OK: in fact, you MUST do that. Note that
请注意,内容视图的宽度现在比父滚动视图的宽度大 225。没关系:事实上,你必须这样做。注意
...you do NOTset the trailing width to negative 225...
...你不设定追踪宽度负225 ...
you would think you have to "match" the widths as you normally would. But if you do that, it will not work at all.
你会认为你必须像往常一样“匹配”宽度。但如果你这样做,它就根本不起作用。
You must set the leading and trailing numbers to ZERO, never negative (even though the width is "bigger")
您必须将前导和尾随数字设置为零,永远不要为负数(即使宽度“更大”)
Interestingly, you can actually set the leading/trailing numbers to any positivevalue (try say "50") and it gives you kind of a margin of the bounce. (It often looks great: try it.) Any negative value on either end will "silently break".
有趣的是,您实际上可以将前导/尾随数字设置为任何正值(尝试说“50”),它为您提供了一定的反弹幅度。(它通常看起来很棒:尝试一下。)任何一端的任何负值都会“悄悄地打破”。
Note that, infuriatingly, often Xcode (as of 7.3.1 anyway),
请注意,令人气愤的是,通常是 Xcode(从 7.3.1 开始),
will 'helpfully' set those values for you to negative numbers!
将为您“帮助”将这些值设置为负数!
because it tries to automatically tally them for you. If so it will silently break. Set all four values to zero in the first instance. And set the width of the content view much wider than the "1000" in the example.
因为它会尝试自动为您统计它们。如果是这样,它将无声地打破。在第一个实例中将所有四个值设置为零。并将内容视图的宽度设置为比示例中的“1000”宽得多。
Edited:I've ended up with using UITableView instead of UIScrollView for most of my requirement. As tableView seems to me much more flexible and dynamic.
编辑:对于我的大部分需求,我最终使用 UITableView 而不是 UIScrollView 。因为 tableView 在我看来更加灵活和动态。
回答by Edward Huynh
I assume you are running into issues with thecontentSize
. Check out this blog poston how to handle the contentSize
when using a "pure" AutoLayout approach. The gist of it is that your constraints implicitly define the content size. You NEVER set it explicitly when using AutoLayout. I've attached example project at the end of the blog post to demonstrate how it works
我假设您遇到了contentSize
. 查看这篇关于如何处理contentSize
使用“纯” AutoLayout 方法时的博客文章。它的要点是您的约束隐式定义了内容大小。使用 AutoLayout 时永远不要明确设置它。我在博客文章的末尾附上了示例项目来演示它是如何工作的
回答by Phan Van Linh
You should organize your layout like thisViewControllerView
contains ScrollView
, ScrollView
contains ContainerView
, ContainerView
contains 2 Labels
你应该像这样组织你的布局ViewControllerView
contains ScrollView
, ScrollView
contains ContainerView
, ContainerView
contains 2Labels
Then follow 3 stepsfor make your ScrollView
can scroll
然后按照 3 个步骤让您ScrollView
可以滚动
- Setting
ScrollView
pin (top/right/bottom/left) toViewControllerView
- Setting
ContainerView
pin (top/right/bottom/left) toScrollView
- Set
Horizontally in Container
(don'tsetVertically in Container
)
- Setting
Label1
pin (top/right/left) toContainerView
Label1
pin (right/left/bottom) toContainerView
and toptoLabel1
- 将
ScrollView
引脚(上/右/下/左)设置为ViewControllerView
- 将
ContainerView
引脚(上/右/下/左)设置为ScrollView
- 设置
Horizontally in Container
(不设置Vertically in Container
)
- 将
Label1
销(上/右/左)到ContainerView
Label1
固定(右/左/下)到ContainerView
和上到Label1
Hope this help
希望这有帮助
回答by Travis
There is a piece in the tech notes that you may have looked over. You can implicitly set the content size of a scroll view using constraints fixed to the edges of the scroll view.
技术说明中有一段您可能已经看过了。您可以使用固定到滚动视图边缘的约束来隐式设置滚动视图的内容大小。
Here's a simple example. Create a storyboard with one view, that has one scroll view. Set that scroll views constraints to make it fit the size of the view you put it in.
这是一个简单的例子。创建一个带有一个视图的故事板,该视图具有一个滚动视图。设置滚动视图约束以使其适合您放入的视图的大小。
Inside that scroll view add a single view. Explicitly set the size of that view using constraints (and make sure that size is bigger than the scroll view).
在该滚动视图中添加一个视图。使用约束显式设置该视图的大小(并确保该大小大于滚动视图)。
Now add four more constraints to that inner view locking the four edges of the inner view to its parent scroll view. Those four constraints will cause the content size to expand to accommodate the inner view.
现在向该内部视图添加四个约束,将内部视图的四个边缘锁定到其父滚动视图。这四个约束将导致内容大小扩展以适应内部视图。
If you have multiple views you want to add to a scroll view, for example laid out horizontally, you'd lock the left side of the first subview to the left of the scroll view, lock the subviews to each other horizontally, and the right side of the last sub view to the right side of the scroll view. Those constraints would force the content size of the scroll view to expand to accommodate all of the subviews and their constraints.
如果您有多个视图要添加到滚动视图中,例如水平布局,则将第一个子视图的左侧锁定到滚动视图的左侧,将子视图水平锁定,然后将右侧的子视图锁定最后一个子视图的一侧到滚动视图的右侧。这些约束将强制滚动视图的内容大小扩展以容纳所有子视图及其约束。
回答by Robert Atkins
If your question is "How do I put a bunch of UITextFields in a vertically scrolling UIScrollView such that they move out of the way of the keyboard when they have focus", the best answer is:
如果您的问题是“如何将一堆 UITextFields 放在垂直滚动的 UIScrollView 中,以便它们在获得焦点时移出键盘”,那么最佳答案是:
Don't.
别。
Use a UITableViewController with static cells instead.
使用带有静态单元格的 UITableViewController 代替。
You get this scroll-out-of-the-way behaviour for free, AND all the content insets Just Work if your view controller is displayed inside a UINavigationController.
如果您的视图控制器显示在 UINavigationController 中,您可以免费获得这种滚动方式,并且所有内容插入都可以正常工作。