xcode iOS 5 SDK 中的容器视图是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13355022/
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
What is Container View in iOS 5 SDK?
提问by William Choi
I'm confused that why and when we need to use the container view? and how can we instantiate a Container View by code?
我很困惑为什么以及何时需要使用容器视图?我们如何通过代码实例化容器视图?
回答by rdelmar
The container view is a view that you can drag into one of your view controllers that you already have in your storyboard (we'll call this viewControllerA). You automatically get a view controller connected to this view via an embed segue. This new view controller has its frame set, so that it's the same size as the container view -- if you resize the container view, the controller will automatically resize as well. So, if you want, you can drag in multiple container views into viewControllerA, and each will have its own view controller. In code, if you need to access these embedded view controllers, they can be accessed from viewControllerA.childViewControllers -- that will give you an array of any embedded view controllers that you have.
容器视图是一种视图,您可以将其拖入故事板中已有的视图控制器之一(我们将其称为 viewControllerA)。您可以通过嵌入转场自动获取连接到此视图的视图控制器。这个新的视图控制器有它的框架集,所以它与容器视图的大小相同——如果你调整容器视图的大小,控制器也会自动调整大小。所以,如果你愿意,你可以将多个容器视图拖入 viewControllerA,每个视图控制器都有自己的视图控制器。在代码中,如果您需要访问这些嵌入式视图控制器,可以从 viewControllerA.childViewControllers 访问它们——这将为您提供您拥有的任何嵌入式视图控制器的数组。
There is a discussion of these container views in the WWDC 2012 Session Videos video called "Adopting Storyboards in Your App".
WWDC 2012 Session Videos 视频中讨论了这些容器视图,名为“在您的应用程序中采用故事板”。
回答by Caleb
I'm confused that why and when we need to use the container view?
我很困惑为什么以及何时需要使用容器视图?
When people talk about container views, they usually mean just a plain old UIView that contains other views. Using a view in that way lets you move all the views it contains as a group, so that their positions relative to each other are maintained. It also makes it easy to hide all the contained views as a group.
当人们谈论容器视图时,他们通常指的是包含其他视图的普通旧 UIView。以这种方式使用视图可以让您将其包含的所有视图作为一个组移动,以便保持它们相对于彼此的位置。它还可以轻松地将所有包含的视图隐藏为一个组。
and how can we instantiate a Container View by code?
我们如何通过代码实例化容器视图?
Same way you'd instantiate a UIView from code normally:
与您通常从代码实例化 UIView 的方式相同:
CGRect frame = CGRectMake(someX, someY, someWidth, someHeight);
UIView *container = [[UIView alloc] initWithFrame:frame];
After that, you'll probably want to add some subviews to the container, and eventually add the container as a subview of your view controller's view.
之后,您可能希望向容器添加一些子视图,并最终将容器添加为视图控制器视图的子视图。
Also, note that we're talking about viewsand not view controllershere. People also talk about container view controllers, by which they mean view controllers that can manage other view controllers. UITabBarController, UINavigationController, and UISplitViewController are examples of the container view controllers that are provided by iOS. You can create your own if you want, but that's a topic for another question.
另外,请注意,我们在这里谈论的是视图而不是视图控制器。人们还谈论容器视图控制器,他们指的是可以管理其他视图控制器的视图控制器。UITabBarController、UINavigationController 和 UISplitViewController 是 iOS 提供的容器视图控制器的示例。如果需要,您可以创建自己的,但这是另一个问题的主题。
Update:From your comment, you're apparently wondering about the "Container View" item in the storyboard editor. If you drag one into a view, you'll see that:
更新:根据您的评论,您显然想知道故事板编辑器中的“容器视图”项目。如果您将一个拖到视图中,您将看到:
The view itself is a UIView used as a placeholder.
Along with the view, the editor creates an area where you can edit the content to be managed by a child view controller. See the picture below.
视图本身是一个用作占位符的 UIView。
与视图一起,编辑器创建一个区域,您可以在其中编辑要由子视图控制器管理的内容。见下图。
This isn't just one object -- it's several. You get a view, a child view controller, and an 'embed' segue. You can certainly create those in code yourself and connect them appropriately if you want to.
这不仅仅是一个对象——它是多个对象。您将获得一个视图、一个子视图控制器和一个“嵌入”转场。如果需要,您当然可以自己在代码中创建它们并适当地连接它们。