在容器视图中加载ViewController

时间:2015-03-04 10:40:15

标签: ios swift storyboard autolayout

我在VC中有一个全屏的containerView。如果我从一个故事板手动添加一个子容器到containerView做一个嵌入segue看起来很好: enter image description here

但如果我按代码嵌入VC:

class BannerContainerVC: UIViewController {

    @IBOutlet weak var container: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let vc = storyboard?.instantiateViewControllerWithIdentifier("test") as UIViewController
        self.container.addSubview(vc.view)
    }
}

我得到了超级奇怪的结果:

enter image description here

6 个答案:

答案 0 :(得分:56)

您需要告诉您的BannerContainer视图控制器它有一个新的子控制器,并告诉Child它有一个父VC。 Apple Docs here中对此进行了描述。像这样:

   [self addChildViewController:vc];
   vc.view.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height);
   [self.container addSubview:vc.view];
   [vc didMoveToParentViewController:self];

或者在Swift中:

    self.addChildViewController(vc)
    vc.view.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height);
    self.container.addSubview(vc.view)
    vc.didMoveToParentViewController(self)

这确保了各种布局和触摸方法传递给子VC;我怀疑你遇到的布局问题可能是由于那些当前没有被调用的方法。

答案 1 :(得分:7)

尝试使用上面的答案,但事实证明var demo = new Vue({ el: '#demo', data: function(){ return { age: '', selectedIndex: '', options: [1,2,3,44,55] }; }, methods: { selected: function () { this.selectedIndex = this.options.indexOf(this.age) alert('this is selected Index ' + this.selectedIndex) } } }) 不再可用。

更新了Swift 3

CGRectMake

答案 2 :(得分:0)

只需调用此函数:-

private func addChildView(viewController: UIViewController, in view: UIView) {
    viewController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    viewController.view.frame = view.bounds
    addChild(viewController)
    view.addSubview(viewController.view)
    viewController.didMove(toParent: self)
 }

答案 3 :(得分:0)

更新Swift 4.2 / 5。

let listVc = ListViewController() 
listVc.view.frame = CGRect(x: 0, y: 0, width: self.listView.frame.width, height: self.listView.frame.height)
addChild(listVc)
listVc.didMove(toParent: self)

ListViewController是要嵌入的另一个视图控制器。

答案 4 :(得分:0)

迅捷5.0 使用此功能可以添加和删除子VC:

private func add(asChildViewController viewController: UIViewController, childFrame:CGRect) {
        // Add Child View Controller
        addChild(viewController)

        // Add Child View as Subview
        view.addSubview(viewController.view)

        // Configure Child View
        viewController.view.frame = childFrame
        viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        // Notify Child View Controller
        viewController.didMove(toParent: self)

    }
    private func remove(asChildViewController viewController: UIViewController) {
        // Notify Child View Controller
        viewController.willMove(toParent: nil)

        // Remove Child View From Superview
        viewController.view.removeFromSuperview()

        // Notify Child View Controller
        viewController.removeFromParent()
    }

来自here

答案 5 :(得分:-3)

更新了Swift 4

self.addChildViewController(vc)
vc.view.frame = CGRect(x: 0, y: 0, width: self.container.frame.size.width, height: self.container.frame.size.height)
self.container.addSubview(vc.view)
vc.didMovestrong text(self)