如何在 iOS 中使用“容器视图”?

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

How to use a 'Container View' in iOS?

iosobjective-cipaduiviewcontrollercontainer-view

提问by The Crazy Chimp

I have noticed the UI Component in XCode: Container View.

我注意到 XCode 中的 UI 组件:Container View.

enter image description here

在此处输入图片说明

Based on the description provided I would like to make use of it to display a reusable component of my app in several different screens. I have been looking around online to try and find a basic tutorial or some documentation on it, however I havent found anything of use.

根据提供的描述,我想利用它在几个不同的屏幕上显示我的应用程序的可重用组件。我一直在网上四处寻找,试图找到一个基本的教程或一些关于它的文档,但是我没有找到任何有用的东西。

Please could someone advise on how to wire this up and make use of it? At the moment it also appears that the container viewdoesn't seem to resize which is problematic. Any tips on how to enable this would be appreciated too.

请有人建议如何连接并使用它?目前看来,container view似乎没有调整大小,这是有问题的。任何有关如何启用此功能的提示也将不胜感激。

Cheers.

干杯。

回答by Alladinian

Well let's break this into some simple steps:

好吧,让我们将其分解为一些简单的步骤:

  1. Drag a container view to the main view of one of your controllers. When you drag the container view, IB will create an embed segue for you and you can resize the container's view by dragging the resize control points as usual. Any changes in the size will be automatically reflected to the view that the segue is pointing to. The idea is that this view will be managed by a different controller, the embedded one. enter image description here

  2. Set your custom subclass as the class of the embedded controller. enter image description here

  3. Now you're good to go. Note that any containers you add will be available through the childViewControllersproperty on your main controller. enter image description here

  1. 将容器视图拖到您的控制器之一的主视图中。当您拖动容器视图时,IB 将为您创建一个嵌入转场,您可以像往常一样拖动调整大小控制点来调整容器视图的大小。大小的任何更改都将自动反映到 segue 指向的视图中。这个想法是这个视图将由不同的控制器管理,嵌入式控制器。 在此处输入图片说明

  2. 将您的自定义子类设置为嵌入式控制器的类。 在此处输入图片说明

  3. 现在你可以走了。请注意,您添加的任何容器都可以通过childViewControllers主控制器上的属性使用。 在此处输入图片说明

This is obviously an oversimplified workflow but it's enough to get you started. For more information on the subject, see Apple's documentation here.

这显然是一个过于简化的工作流程,但足以让您入门。有关该主题的更多信息,请参阅此处的 Apple 文档