2

我有一个UIStackView并且我正在动态添加UIViewControllers包含,这是我的代码;

[self addChildViewController:driverForm];
[self addChildViewController:marketingView];

[self.stackView insertArrangedSubview:driverForm.view atIndex:0];
[self.stackView insertArrangedSubview:marketingView.view atIndex:1];

[driverForm didMoveToParentViewController:self];
[marketingView didMoveToParentViewController:self];

阅读文件后,我必须致电didMoveToParentViewController

我面临的问题是,最终的动作UIViewController没有被调用,但是第一个 ViewController 被调用了。如果我交换这些回合,第一个有效,最后一个无效。

4

3 回答 3

2

只需将 ViewController 的视图添加到 UIStackView 中,如下所示:

yourStackView.addArrangedSubview(yourViewController.view)

此外,您不必担心视图为零,因为它总是返回 UIView!

注意顺序是颠倒的,所以最后一个出现在最前面。为了解决这个问题,假设您有一个视图控制器数组,您可以使用stride反向遍历您的数组并将视图控制器添加到您的堆栈中。

于 2018-04-20T12:07:13.193 回答
1

这是 Swift 5 的快速复制/意大利面版本:

private var childViewController: UIViewController
private var stackView: UIStackView?

// MARK: - UIViewController
    
override func loadView() {
    super.loadView()
    
    // 1. Add the child view controller to the parent.
    addChild(childViewController)
    
    // 2 Create and add the stack view containing child view controller's view.
    stackView = UIStackView(arrangedSubviews: [childViewController.view])
    stackView!.axis = .vertical
    self.view.addSubview(stackView!)
    
    // 3. Let the child know that it's been added!
    childViewController.didMove(toParent: self)
}
于 2020-08-11T20:03:11.687 回答
-3

UIStackView 用于在同一个 UIViewController 类中安排多个子视图。如何将它用于不同的 UIViewControllers?

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStackView_Class_Reference/#//apple_ref/doc/uid/TP40015256-CH1-SW31

于 2016-05-04T11:00:24.443 回答