xcode UIView 与容器视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24859824/
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
UIView vs Container View
提问by Alix
So here is the problem I am trying to solve.
所以这是我试图解决的问题。
In each
viewController
I am trying to insert ads and the actual control elements. I finished couple of tutorial on raywenderlinch.com to understand that how people professionally put ads in their app. They used UIViews to have two views under mainview of view controller. So I completely understood that onesubview
hold the ads and another is holding actual app contents. if Ad is loaded take up the screen or else let other view have all available area.
在每个
viewController
我试图插入广告和实际控制元素。我在 raywenderlinch.com 上完成了几个教程,以了解人们如何专业地在他们的应用程序中放置广告。他们使用 UIViews 在视图控制器的主视图下有两个视图。所以我完全理解一个subview
持有广告,另一个持有实际的应用程序内容。如果加载广告占据屏幕,否则让其他视图拥有所有可用区域。
After I came back to xcode I started coding the way I learned there. but when I was dropping UIView
on storyboard, I saw containerView
, which I think was not present when the tutorial was written.
So I am here to ask about the both approach and their pros and cons.
So basically its UIView
vs ContainerView
. Which way I should do, and why ?
Any help would be greatly appreciated.
在我回到 xcode 之后,我开始按照我在那里学到的方式进行编码。但是当我UIView
在情节提要上时,我看到了containerView
,我认为在编写教程时不存在。
所以我在这里询问这两种方法及其优缺点。
所以基本上它的UIView
vs ContainerView
. 我应该怎么做,为什么?
任何帮助将不胜感激。
回答by Keenle
You use UIView
when you already have a view and you do not need to have a dedicated view controller to build and handle interactions within it.
UIView
当您已经拥有一个视图并且不需要专用的视图控制器来构建和处理其中的交互时,您可以使用它。
From the UIViewhelp page:
从UIView帮助页面:
UIView object claims a rectangular region of its enclosing
superview
(its parent in the view hierarchy) and is responsible for all drawing in that region ...
UIView 对象声明其封闭的矩形区域
superview
(其在视图层次结构中的父级)并负责该区域中的所有绘图......
Simplified structure:YourViewController---(has)--->UIView
简化结构:YourViewController ---(has)---> UIView
You use UIContainerView
when you need to embed another view controller in the one that you already have. The embedded view controller is in charge of returning a view for the region that the UIViewContainer
occupies. Therefore, your UIContainerView
knows which view controller to use to render UIView
inside the region it occupies.
您可以使用UIContainerView
,当你需要在一个你已经拥有嵌入另一个视图控制器。嵌入式视图控制器负责返回所UIViewContainer
占据区域的视图。因此,您UIContainerView
知道使用哪个视图控制器UIView
在它占据的区域内进行渲染。
From the UIContainerViewhelp page:
从UIContainerView帮助页面:
Container View defines a region within a view controller's view subgraph that can include a child view controller.
容器视图在视图控制器的视图子图中定义了一个区域,该区域可以包含一个子视图控制器。
Simplified structure:YourViewController---(has)--->SubViewController---(has)--->UIView
简化结构:YourViewController ---(has)---> SubViewController ---(has)---> UIView
That SubViewController returns a view and handles its events.
该 SubViewController 返回一个视图并处理其事件。
回答by Dave G
As an alternative answer, you can also consider the use case instead of the technical differences. For example: Why use a container view?
作为替代答案,您还可以考虑用例而不是技术差异。例如:为什么使用容器视图?
A common use for container views is to reuse (share) a view, directly in the storyboard. Previously, reusing a view required creating a separate "xib" file, and programmatically adding that view when the view controller was loaded.
容器视图的一个常见用途是直接在故事板中重用(共享)视图。以前,重用视图需要创建一个单独的“xib”文件,并在加载视图控制器时以编程方式添加该视图。
The above image is from thisextremely simple, easy to follow guide that walks you through how to setup a container view that is shared between 2+ view controllers.
上图来自这个极其简单、易于遵循的指南,该指南将引导您了解如何设置在 2 个以上视图控制器之间共享的容器视图。
A few other thoughts on when to use it:
关于何时使用它的一些其他想法:
- A navigation bar is part of a UINavigationController, which is a container view controller. So, if you wanted to build a custom alternative, you'd probably use a container view.
- A container might help anytime that you want to temporarily show a complex view on top of your current VC but can't/don't want to present another VC modally. This approach still allows you to build that temporary view in interface builder, to setup auto layout constraints for it, etc
- I also found a guideexplaining that there's a way to switch out different container views based on the situation, allowing your VC to have sub-sections which are very dynamic, yet without having to build those sub-sections programmatically. A picture, from that guide, exhibiting what I'm referring to:
- 导航栏是 UINavigationController 的一部分,它是一个容器视图控制器。因此,如果您想构建自定义替代方案,您可能会使用容器视图。
- 当您想在当前 VC 之上临时显示复杂视图但不能/不想以模态方式呈现另一个 VC 时,容器可能会有所帮助。这种方法仍然允许您在界面构建器中构建临时视图,为其设置自动布局约束等
- 我还找到了一个指南,解释了有一种方法可以根据情况切换不同的容器视图,允许您的 VC 拥有非常动态的子部分,而无需以编程方式构建这些子部分。来自该指南的图片展示了我所指的内容:
Hopefully this helps people who are trying to figure out when a container view applies to them. If you have other example use cases, please edit/add them or leave them in the comments!
希望这可以帮助那些试图弄清楚容器视图何时适用于他们的人。如果您有其他示例用例,请编辑/添加它们或将它们留在评论中!
回答by Shubhendu
If you see in detail these container view of UIView class type. To get the insights of why we need containerView you should see below portion
如果您详细查看这些 UIView 类类型的容器视图。要了解为什么我们需要 containerView,您应该看到以下部分
In most ways, a container view controller is just like a content view controller. It manages views and content, coordinates with other objects in your app, and responds to events in the responder chain. Before designing a container controller, you should already be familiar with designing content view controllers. The design questions in “Creating Custom Content View Controllers” also apply when creating containers.
在大多数情况下,容器视图控制器就像内容视图控制器。它管理视图和内容,与应用程序中的其他对象协调,并响应响应者链中的事件。在设计容器控制器之前,您应该已经熟悉设计内容视图控制器。“创建自定义内容视图控制器”中的设计问题也适用于创建容器。
for more detail about container view goto linkBut before you begin you should have an understanding of
有关容器视图的更多详细信息,请转到链接但在开始之前,您应该了解
and also you can check this tutorialfor how to use container view.
您也可以查看本教程以了解如何使用容器视图。
Thus you can go for both the approaches. Hope this will help you. happy coding :)
因此,您可以同时采用这两种方法。希望这会帮助你。快乐编码:)