以编程方式添加Container

以编程方式添加Container

本文介绍了如何以编程方式添加Container View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以通过界面编辑器轻松地将容器视图添加到故事板中。添加时,容器视图是占位符视图,嵌入segue和(子)视图控制器。

A Container View can be easily added into a storyboard through Interface Editor. When added, a Container View is of a placeholder view, an embed segue, and a (child) view controller.

但是,我无法找到方法以编程方式添加Container View。实际上,我甚至找不到一个名为 UIContainerView 的类。

However, I am not able to find a way to add a Container View programmatically. Actually, I am not even able to find a class named UIContainerView or so.

该类的名称Container View无疑是一个良好的开端。包括segue在内的完整指南将非常受欢迎。

A name for the class of Container View is surely a good start. A complete guide including the segue will be much appreciated.

推荐答案

故事板容器视图只是一个标准的 UIView 对象。没有特殊的容器视图类型。实际上,如果查看视图层次结构,可以看到容器视图是标准的 UIView

A storyboard "container view" is just a standard UIView object. There is no special "container view" type. In fact, if you look at the view hierarchy, you can see that the "container view" is a standard UIView:

要以编程方式实现此目的,请使用视图控制器包含:

To achieve this programmatically, you employ "view controller containment":


  • 通过在storyboard对象上调用 instantiateViewController(withIdentifier :) 来实例化子视图控制器。

  • 在父视图控制器中调用 addChildViewController

  • 添加视图控制器的视图使用 addSubview 进入视图层次结构(并根据需要设置 frame 或约束)。

  • 在子视图控制器上调用 didMove(toParentViewController:)方法,将引用传递给父视图控制器。

  • Instantiate the child view controller by calling instantiateViewController(withIdentifier:) on the storyboard object.
  • Call addChildViewController in your parent view controller.
  • Add the view controller's view to your view hierarchy with addSubview (and also set the frame or constraints as appropriate).
  • Call the didMove(toParentViewController:) method on the child view controller, passing the reference to the parent view controller.

参见。

See Implementing a Container View Controller in the View Controller Programming Guide and the "Implementing a Container View Controller" section of the UIViewController Class Reference.

例如,在Swift 3中它可能看起来像:

For example, in Swift 3 it might look like:

override func viewDidLoad() {
    super.viewDidLoad()

    let controller = storyboard!.instantiateViewController(withIdentifier: "Second")
    addChildViewController(controller)
    controller.view.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(controller.view)

    NSLayoutConstraint.activate([
        controller.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
        controller.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
        controller.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
        controller.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10)
    ])

    controller.didMove(toParentViewController: self)
}

注意,上面没有实际上在层次结构中添加了一个容器视图。如果你想这样做,你可以这样做:

Note, the above doesn't actually add a "container view" to the hierarchy. If you want to do that, you'd do something like:

override func viewDidLoad() {
    super.viewDidLoad()

    // add container

    let containerView = UIView()
    containerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(containerView)
    NSLayoutConstraint.activate([
        containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
        containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
        containerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
        containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
    ])

    // add child view controller view to container

    let controller = storyboard!.instantiateViewController(withIdentifier: "Second")
    addChildViewController(controller)
    controller.view.translatesAutoresizingMaskIntoConstraints = false
    containerView.addSubview(controller.view)

    NSLayoutConstraint.activate([
        controller.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
        controller.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
        controller.view.topAnchor.constraint(equalTo: containerView.topAnchor),
        controller.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
    ])

    controller.didMove(toParentViewController: self)
}

如果在不同的子视图控制器之间进行转换,并且您只是想确保一个子视图位于同一位置和前一个子视图中,则后一种模式非常有用(即放置的所有唯一约束都由容器视图决定,而不是每次都需要重建这些约束。但是如果只是执行简单的视图包含,那么对这个单独的容器视图的需求就不那么引人注目了。

This latter pattern is extremely useful if ever transitioning between different child view controllers and you just want to make sure one child's view is in the same location and the previous child's view (i.e. all the unique constraints for the placement are dictated by the container view, rather than needing to rebuild these constraints each time). But if just performing simple view containment, the need for this separate container view is less compelling.

对于Swift 2的再现,请参阅。

For Swift 2 renditions, see previous revision of this answer.

这篇关于如何以编程方式添加Container View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:36