iOS的7自动版式视图中滚动查看镶嵌 [英] iOS 7 autolayout of view embeded in scrolling view

查看:215
本文介绍了iOS的7自动版式视图中滚动查看镶嵌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面可能是,从iOS的新手下一个愚蠢的问题,我正在尝试为滚动和经销商布局的东西。

Here is probably, next stupid question from iOS newbie, what I'm experimenting is scrolling and auto layout stuff.

所以我创建故事板并添加嵌入式滚动视图中的一个视图。视图有白色的背景和我设置的滚动视图的背景是蓝色:

So I've created storyboard and added one view embedded by scrolling view. View has white background and I set scrolling view background to be blue:

约束你看到所有的有0值,以匹配父。
在人像模式样样精:

Constraints you see all have 0 value, to match parent. In portrait mode everything fine:

但在没有景观,叶观不改变大小和滚动视图(右侧蓝色的区域变得可见)

But in landscape not, the leaf view not resized and scrolling view (blue one area at the right becomes visible)

推荐答案

与滚动视图的技巧在于你滚动视图及其子视图之间添加了自动布局的限制将决定 contentSize 滚动视图的,而不是子视图本​​身的大小。请参见技术说明TN2154 - 的UIScrollView和自动布局。这实际上是一个非常有用的功能(我们不再需要手工计算/调整 contentSize ),但它使直观,使基于屏幕的尺寸的子视图大小调整。

The trick with scroll views is that the auto-layout constraints you add between the scroll view and its subviews will dictate the contentSize of the scroll view, not the size of the subviews themselves. See Technical Note TN2154 - UIScrollView And Autolayout. This actually is an incredibly useful feature (we no longer have to manually calculate/adjust contentSize), but it makes it unintuitive to make a subview resize based upon the screen's dimensions.

如果您希望滚动视图的子视图,你从纵向到横向改变它的大小,你可以添加滚动视图的子视图和(子视图和它的父的上海华之间即)根视图之间的约束。这并不能代替其子视图(您仍然需要决定了滚动视图 contentSize ),但补充滚动视图之间的约束它,这样子视图改变宽度根视图一样。

If you want the scroll view's subview to change its size as you go from portrait to landscape, you can add a constraint between the scroll view's subview and the root view (i.e. between the subview and its superview's superview). This doesn't replace the constraints between the scroll view and its subviews (which you still need to dictate the scroll view's contentSize), but supplements it so that the subview changes width as the root view does.

您可以通过展开文档大纲和<大骨节病>控制做到这一点 -dragging(或右键单击并拖动)从子视图到根视图:

You can do this by expanding the document outline and control-dragging (or right-click-dragging) from the subview to the root view:

您可以再指定,使它们的宽度等于:

You can then specify to make their widths equal:

当你做到这一点,滚动视图的子视图会改变宽度为导向的变化。这里是 recursiveDescription 200点高的子视图时,该设备是纵向:

When you do this, the scroll view's subview will change width as the orientation changes. Here is the recursiveDescription of a 200 point tall subview when the device was in portrait:

(lldb) po [[UIWindow keyWindow] recursiveDescription]
<UIWindow: 0x8cbba70; frame = (0 0; 320 480); autoresize = W+H; gestureRecognizers = <NSArray: 0x8cbbca0>; layer = <UIWindowLayer: 0x8cbb4a0>>
   | <UIView: 0x8cbd990; frame = (0 0; 320 480); autoresize = RM+BM; layer = <CALayer: 0x8cbd040>>
   |    | <UIScrollView: 0x8cbdb70; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x8cc19a0>; layer = <CALayer: 0x8cbd440>; contentOffset: {0, 0}>
   |    |    | <UIView: 0x8cbdf00; frame = (0 0; 320 200); autoresize = W+H; layer = <CALayer: 0x8cbdf60>>

当我走进风景,去年子视图的宽度自动改变:

And when I went into landscape, that last subview's width changed automatically:

(lldb) po [[UIWindow keyWindow] recursiveDescription]
<UIWindow: 0x8cbba70; frame = (0 0; 320 480); autoresize = W+H; gestureRecognizers = <NSArray: 0x8cbbca0>; layer = <UIWindowLayer: 0x8cbb4a0>>
   | <UIView: 0x8cbd990; frame = (0 0; 320 480); transform = [0, -1, 1, 0, 0, 0]; autoresize = RM+BM; layer = <CALayer: 0x8cbd040>>
   |    | <UIScrollView: 0x8cbdb70; frame = (0 0; 480 320); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x8cc19a0>; layer = <CALayer: 0x8cbd440>; contentOffset: {0, 0}>
   |    |    | <UIView: 0x8cbdf00; frame = (0 0; 480 200); autoresize = W+H; layer = <CALayer: 0x8cbdf60>>

显然,你可以用高度同样的事情了。在这个例子中,我刚刚有了一个高度约束固定的高度为200磅,但做任何你想要的。

Clearly, you can do the same thing with the height, too. In this example, I just had a height constraint that fixed the height at 200 pt, but do whatever you want.

您可以显然也走老派和响应布局事件以编程方式调整子视图的制约,但以上技术不再需要做到这一点。

You can obviously also go "old school" and respond to layout events to programmatically adjust constraints of subviews, but the above technique eliminates the need to do that.

这篇关于iOS的7自动版式视图中滚动查看镶嵌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆