Switch container view dynamically

前端 未结 3 669
说谎
说谎 2020-12-08 15:21

I\'m using a storyboard container view. So I have a view controller containing a container view. I have a second, smaller view in the storyboard, which is embedded in that c

3条回答
  •  再見小時候
    2020-12-08 16:01

    If you deal with more than 2 child view controllers it's a good idea to store them in array. Also, you'll need to apply constraints every time you add a new subview to the container view. Here's a modified version of the accepted answer (using Swift 2 syntax):

    import UIKit
    
    class FooBarViewController: UIViewController
    {
        // MARK: - IBOutlets
    
        @IBOutlet weak var containerView: UIView!
    
        // MARK: - Properties
    
        var vcs = [UIViewController]()
        var currentVC: UIViewController!
    
        // MARK: - ViewController Lifecycle
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
    
            vcs.append(childViewControllers.last!)
            vcs.append(storyboard!.instantiateViewControllerWithIdentifier("FooViewControler"))       
            vcs.append(storyboard!.instantiateViewControllerWithIdentifier("BarViewController"))
            currentVC = vcs[0]
    
            for vc in vcs {
                vc.view.frame = containerView.bounds
            }
        }
    
        // MARK: - Methods
    
        func switchToViewController(targetVC: UIViewController)
        {
            addChildViewController(targetVC)
            currentVC.willMoveToParentViewController(nil)
    
            transitionFromViewController(
                currentVC,
                toViewController: targetVC,
                duration: 0.0, // 0.3
                options: .TransitionNone, // .TransitionCrossDissolve,
                animations: nil,
                completion: { finished in
                    self.currentVC.removeFromParentViewController()
                    targetVC.didMoveToParentViewController(self)
                    self.currentVC = targetVC
                    self.applyConstraints()
            })
        }
    
        func applyConstraints()
        {
            let viewsDict = ["newSubview": currentVC.view]
    
            currentVC.view.translatesAutoresizingMaskIntoConstraints = false
    
            containerView.addConstraints(
                NSLayoutConstraint.constraintsWithVisualFormat("H:|[newSubview]|",
                    options: NSLayoutFormatOptions(rawValue: 0),
                    metrics: nil,
                    views: viewsDict))
    
            containerView.addConstraints(
                NSLayoutConstraint.constraintsWithVisualFormat("V:|[newSubview]|",
                    options: NSLayoutFormatOptions(rawValue: 0),
                    metrics: nil, views:
                    viewsDict))
        }
    
        // MARK: - IBActions
    
        @IBAction func switchControllers(sender: UISegmentedControl)
        {
            let i = sender.selectedSegmentIndex
    
            if currentVC != vcs[i] {
                self.addChildViewController(vcs[i])
                switchToViewController(vcs[i])
            }
        }
    }
    

提交回复
热议问题