Embed UIViewController Programmatically?

前端 未结 2 1622
盖世英雄少女心
盖世英雄少女心 2020-12-28 14:12

I have a Storyboard setup with a UIViewController with an container view so that I can embed another UIViewController inside of it.

In a ce

相关标签:
2条回答
  • 2020-12-28 14:25

    You can do this by programmatically, below is the method which will take a bool value to make decision which view controller need to be added in container view and then will instantiate an object and after that will add it to containerView

    - (void)addViewControllerToContainerView:(BOOL)addVC1
    {
    // Get storyboard
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"<name of storyboard>" bundle:[NSBundle mainBundle]];
        UIViewController *viewController = nil;
        if (addVC1)
        {
    // get viewController with identifier 
            viewController = [storyBoard instantiateViewControllerWithIdentifier:@"<View Controller 1 Identifier>"];
        }
        else
        {
            viewController = [storyBoard instantiateViewControllerWithIdentifier:@"<View Controller 2 Identifier>"];
        }
    // lets add it to container view
        [viewController willMoveToParentViewController:self];
        [self.view addSubview:viewController.view];
        [self addChildViewController:viewController];
        [viewController didMoveToParentViewController:self];
    // keep reference of viewController which may be useful when you need to remove it from container view, lets consider you have a property name as containerViewController
        self.containerViewController = viewController;
    }
    

    When you need remove view controller from container view controller you can do this

       [self.containerViewController willMoveToParentViewController:nil];  // 1   
       self.containerViewController.view removeFromSuperView];
       [self.containerViewController removeFromParentViewController];//this line is updated as view is removed from parent view cotnroller istead of its viewcontroller is removed from parentViewController 
       self.containerViewController = nil
    

    Apple docs about container view controllers

    0 讨论(0)
  • 2020-12-28 14:30

    Swift version with UIPageViewController (which I believe is a common use case for this)

    let pageController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    // do your PageViewController stuff here (delegate and dataSource)
    pageController.willMove(toParent: self)
    containerView.addSubview(pageController.view)
    pageController.view.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
                pageController.view.topAnchor.constraint(equalTo: containerView.topAnchor),
                pageController.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
                pageController.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
                pageController.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor)
    ])
    addChild(pageController)
    pageController.didMove(toParent: self)
    

    I didn't figure out how to make use of the UIPageControll included in UIPageViewController so I ended up using my own instance. But I believe this is out of the scope of this question.

    0 讨论(0)
提交回复
热议问题