在iOS中,使用storyboard,如何在容器视图中设置视图控制器?

问题描述:

我在主故事板中创建并绘制了一个名为AutocompleteVC的自定义UIViewController。 AutocompleteVC将用于几个不同的地方/故事板并具有不同的尺寸。

I have created and drawn out a custom UIViewController called AutocompleteVC in my Main storyboard. AutocompleteVC will be used in several different places/storyboards and with different dimensions.

例如,在我的Transit故事板中,如下所示,有一个名为TransitVC的自定义UIViewController,显示在左侧。在TransitVC中,我有一个带有名为autocompleteContainerView的IBOutlet的容器​​视图。 autocompleteContainerView有一个名为autocompleteEmbedSegue的segue到一个通用的UIViewController,右边显示为红色。

For example, in my Transit storyboard, shown below,, there is a custom UIViewController called TransitVC, shown on the left. In TransitVC, I have a container view with an IBOutlet called autocompleteContainerView. autocompleteContainerView has a segue called autocompleteEmbedSegue to a generic UIViewController, shown on the right in red.

目标是让TransitVC在autocompleteContainerView中保存AutocompleteVC。但我无法让这个工作。我想在容器视图中执行此操作的原因是我可以使用autolayout来设置它的约束。否则,我知道如何在代码中完全执行此操作。

The goal is to have TransitVC hold AutocompleteVC inside autocompleteContainerView. But I'm having trouble getting this to work. The reason I want to do this inside a container view is so I can use autolayout to set constraints on it. Otherwise, I know how to do this purely in code.

我相信我的方法可能存在缺陷。如果我想最大限度地利用故事板,那么正确的方法是什么。

I believe my approach might be flawed. What is the correct approach to do this if I want to maximize storyboard usage.

我不确定你在问什么。与容器视图建立父/子关系非常简单,正如您所概述的那样。只需在父视图控制器内创建一个容器视图,创建子视图控制器场景,然后从容器视图控制拖动到子视图控制器以创建嵌入segue。

I'm not sure what you are asking. Setting up a parent/child relationship with a container view is very easy, exactly as you have outlined. Just create a container view inside the parent view controller, create the child view controller scene, and then control-drag from the container view to the child view controller to create the embed segue.