如何使用一个 ContainerView 显示多个 ViewController [英] How to use one ContainerView to show multiple ViewControllers

查看:31
本文介绍了如何使用一个 ContainerView 显示多个 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的场景是现在我的应用程序使用 12 个 ViewController.每个都有两个 ContainerViews,它们包含相同的一组控件(我将它们称为控件组),我通过 segues 显示它们.到目前为止,这是有效的,但是我必须为相同的两个 ContainerView 布局和设置约束 12 次 - 每个 VC 一次.如果我添加另一个 VC,那么我必须重新布置它们,等等.如果我更改了布局的任何部分,那么我必须一次又一次地修改所有 12 个 - 这不是真正可扩展的.

My scenario is that right now my app uses 12 ViewControllers. Each one has two ContainerViews which hold the same set of controls (I'll call them control groups) and I show them via segues. So far this is working however, I have to layout and set constraints for the same two ContainerViews 12 times - once for each VC. And if I add another VC, then I have to lay them out again, etc. If I change any part of my layout then I have to modify all 12 again and again - it's not really scalable.

在我看来,如果两个控制组位于一个还包含 ContainerView 的 VC 上会更容易.那么,如果 ContainerView 可以在十二个视图中的每一个之间切换,那么维护起来就会容易得多.

It seems to me it would be easier if the two control groups are on one VC which also contains a ContainerView. Then, if the ContainerView could switch between each of the twelve views it would be a lot easier to maintain.

例如这样的模型:

此模型显示了同一 VC 上的两个控制容器Upper"和Lower".中间是 ContainerView,它将显示所需的 VC.

This mockup shows the two control containers "Upper" and "Lower" on the same VC. in between would be the ContainerView which would show the required VC.

当然,ContainerViews 只能嵌入一个 VC,所以我见过的最接近的解决方案是将 12 个 ContainerViews 放在一起 - 看起来仍然很混乱.

Of course, ContainerViews can only embed a single VC, so the closest solution I've seen is to put 12 ContainerViews on top of each other - seems quite messy still.

我不一定需要使用 ContainerView - 但是,有一些不错的功能,例如限制 VC 的大小并使其符合其尺寸,这使得布局内容更容易.

I'm not necessarily requiring the use of a ContainerView - however, there are some nice features such as restricting the size of the VC and making it adhere to its dimensions which makes laying out the contents easier.

我怎样才能获得这种结构和行为?(注意:我也希望能够使用 Storyboard)

How can I get this kind of structure and behavior? (Note: I would like to be able to use the Storyboard too)

推荐答案

来自我正在工作的项目:

From the project I am working:

向主视图控制器添加任意数量的容器视图.比在您的代码中,更改 alpha 值以显示/隐藏它们.像这样:

Add as many container views as you want to your main view controller. Than in your code, change the alpha values to show/hide them. Like this:

@IBOutlet weak var timelineContainerView: UIView!
@IBOutlet weak var albumsContainerView: UIView!


@IBAction func journeySegmentedControlValueChanged(_ sender: UISegmentedControl) {
    switch sender.selectedSegmentIndex {
    case 0:
        timelineContainerView.alpha = 1.0
        albumsContainerView.alpha = 0.0
    case 1:
        timelineContainerView.alpha = 0.0
        albumsContainerView.alpha = 1.0
    default:
        break
    }
}

如果您有很多控制器,您可能需要使用 IBOutlet 组.

If you have a lot of controllers, you may want to use an IBOutlet group.

这篇关于如何使用一个 ContainerView 显示多个 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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