xcode 如何在选中 Autolayout 的情况下使用 ScrollView 和 ContainerView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20218870/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:21:04  来源:igfitidea点击:

How to use ScrollView and ContainerView with Autolayout checked

iosxcodeuiscrollviewios7xcode5

提问by Marshall

I have trouble implementing scrollview with auto layout:/

我在使用自动布局实现滚动视图时遇到问题:/

Here is simple "Hello World" project with that problem: http://www.sendspace.com/file/cg96by

这是带有该问题的简单“Hello World”项目:http: //www.sendspace.com/file/cg96by

But the problem… I need to create scrollview, but also I need to use auto layout. So I created pure single view application, added (like I saw in some tutorials) Scrollview (the same size as main view), added constraints (all zeros) and then add Container View (the same size as scroll view), add constraints and.. It's didn't work:/ It doesn't scroll. Here is the screen of that situation: enter image description here

但问题是……我需要创建滚动视图,但我还需要使用自动布局。所以我创建了纯单视图应用程序,添加(就像我在一些教程中看到的)滚动视图(与主视图大小相同),添加约束(全为零),然后添加容器视图(与滚动视图相同的大小),添加约束和..它没有用:/它不滚动。这是这种情况的屏幕: 在此处输入图片说明

Now, when I delete two constraints named: Center X and Y Alignment (marked on the screen), it works perfectly, but Xcode5 doesn't quite like it :/ and shows some errors like on the next screen below.

现在,当我删除两个名为:Center X 和 Y Alignment(标记在屏幕上)的约束时,它运行良好,但 Xcode5 不太喜欢它:/ 并在下面的下一个屏幕上显示一些错误。

Why does he want width and heigh 0 ?!

为什么他想要宽度和高度 0 ?!

Actually ContainerView doesn't matter. I've also tried with image view. When you select "Add missing constraints" it also add Center X and Y Alignment. And scroll doesn't work. When you delete them, scroll works but you have this warring:

实际上 ContainerView 并不重要。我也试过图像视图。当您选择“添加缺少的约束”时,它还会添加中心 X 和 Y 对齐。滚动不起作用。当你删除它们时,滚动工作但你有这个交战:

enter image description here

在此处输入图片说明

回答by JAManfredi

Ok, I find manually adding the autolayout constraints to be quite confusing, using the "Add New Constraints" button at the bottom really comes in handy, try this, after you get each view where you want it use that way of adding layout constraints for each view (ie scrollView, containerView, UIImageView) I did this, and heres the example, select each constraint as I have so that the top, left, bottom, and right edges are set, this should do exactly what you want.

好的,我发现手动添加自动布局约束非常令人困惑,使用底部的“添加新约束”按钮真的很方便,试试这个,在你获得每个你想要的视图后使用这种方式添加布局约束每个视图(即scrollView、containerView、UIImageView)我都这样做了,这是示例,选择每个约束,以便设置顶部、左侧、底部和右侧边缘,这应该完全符合您的要求。

You will probably want to erase all your current constraints and then implement them this way.

您可能希望清除所有当前约束,然后以这种方式实现它们。

heres the layout like you want http://cl.ly/image/463k2043401L

这是您想要的布局http://cl.ly/image/463k2043401L

and heres the example adding the constraints: http://cl.ly/image/472l2V0l3N1L

这是添加约束的示例:http: //cl.ly/image/472l2V0l3N1L

To keep it dynamic so that you can rotate it, you will want to do the above for all view BUT the container view... then If you control drag from container to scrollview with no constraints, you can add all of them that it suggests except for the height and width at the bottom, that satisfies any missing constraints and should do the trick, It will center the container inside of the scrollview and keep it pinned to the top, bottom, left and right, and it should dynamically change with your scrollview content size.

为了保持动态以便您可以旋转它,您需要对所有视图执行上述操作,但容器视图...然后如果您控制从容器到滚动视图的拖动而没有限制,您可以添加它建议的所有内容除了底部的高度和宽度,满足任何缺少的约束并且应该可以解决问题,它将使滚动视图内部的容器居中并将其固定在顶部,底部,左侧和右侧,并且它应该随着您的滚动视图内容大小。

http://cl.ly/image/1o3k1e452W0g

http://cl.ly/image/1o3k1e452W0g

Sorry for the confusion, hope this helps!

抱歉造成混乱,希望对您有所帮助!

回答by vedrano

I had a similar problem and i found relative simple solution from within Interface Builder using pure Autolayout without any code.

我遇到了类似的问题,我在 Interface Builder 中使用纯自动布局找到了相对简单的解决方案,无需任何代码。

For proof-of-concept at first remove any constraint in View Controller to if see this works.

对于概念验证,首先删除视图控制器中的任何约束,如果看到这有效。

This is sample layout:

这是示例布局:

  • View (main view of my UIViewController)

    • Scroll View (UIScrollView)

      • Container View (UIView)

        • Content View (e.g. UIImageView)
  • 视图(我的 UIViewController 的主视图)

    • 滚动视图 (UIScrollView)

      • 容器视图 (UIView)

        • 内容视图(例如 UIImageView)

A. Scroll View width/height should be smaller that Container View width/height

A. Scroll View 的宽度/高度应该小于 Container View 的宽度/高度

B. Container View should have some determinated width/height (may be explicit width/height )

B. Container View 应该有一些确定的宽度/高度(可能是显式的宽度/高度)

C. Do Control-drag Container View to Scroll View and add only:

C. 按住 Control 拖动 Container View 到 Scroll View 并只添加:

  1. Leading Space to Container
  2. Trailing Space to Container
  1. 引领空间到集装箱
  2. 到容器的尾随空间

D. Check out those two constraints and set "constant" value for both to 0

D. 检查这两个约束并将两者的“常量”值设置为 0

E. Run app and

E. 运行应用程序和