addChildViewController 方法只能用于将子视图控制器添加到 containerViewController 吗?
addChildViewController method is only for adding child viewControllers to containerViewController?
我看到Apple中对这个方法的描述是这样说的
func addChildViewController(_ childController: UIViewController)
This method is only intended to be called by an implementation of a custom container view controller. If you override this method, you must call super in your implementation.
我明白了,这么多例子,人们到处都使用 addChildViewController
而没有 containerViewController
。
例如:我没有使用containerView。我在下面添加了?这是正确的吗?
// Create child VC
let childVC = UIViewController()
// Set child VC
self.addChildViewController(childVC)
// Add child VC's view to parent
self.view.addSubview(childVC.view)
// Register child VC
childVC.didMove(toParentViewController: self)
// Setup constraints for layout
childVC.view.translatesAutoresizingMaskIntoConstraints = false
childVC.view.topAnchor.constraint(equalTo: heroView.bottomAnchor).isActive = true
childVC.view.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
childVC.view.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
childVC.view.heightAnchor.constraint(equalToConstant: height).isActive = true
如文档所述,该方法旨在供可以包含另一个视图控制器的视图控制器使用。一个例子是导航和标签栏控制器。
如果您实现了一个自定义控制器,例如,将一个控制器放在屏幕的上半部分和下半部分,当您设置 bottomHalfViewController
属性 时,您会调用 addChildViewController
方法让您的控制器知道它应该在子视图控制器时处理它。
这意味着它将转发所有视图生命周期调用,如 viewWillAppear:
我看到Apple中对这个方法的描述是这样说的
func addChildViewController(_ childController: UIViewController)
This method is only intended to be called by an implementation of a custom container view controller. If you override this method, you must call super in your implementation.
我明白了,这么多例子,人们到处都使用 addChildViewController
而没有 containerViewController
。
例如:我没有使用containerView。我在下面添加了?这是正确的吗?
// Create child VC
let childVC = UIViewController()
// Set child VC
self.addChildViewController(childVC)
// Add child VC's view to parent
self.view.addSubview(childVC.view)
// Register child VC
childVC.didMove(toParentViewController: self)
// Setup constraints for layout
childVC.view.translatesAutoresizingMaskIntoConstraints = false
childVC.view.topAnchor.constraint(equalTo: heroView.bottomAnchor).isActive = true
childVC.view.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
childVC.view.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
childVC.view.heightAnchor.constraint(equalToConstant: height).isActive = true
如文档所述,该方法旨在供可以包含另一个视图控制器的视图控制器使用。一个例子是导航和标签栏控制器。
如果您实现了一个自定义控制器,例如,将一个控制器放在屏幕的上半部分和下半部分,当您设置 bottomHalfViewController
属性 时,您会调用 addChildViewController
方法让您的控制器知道它应该在子视图控制器时处理它。
这意味着它将转发所有视图生命周期调用,如 viewWillAppear: