addChild not working when children vc not in same storyboard as parent vc

200 Views Asked by At

I'm learning and trying a lot at the moment and so I tried to do a custom tab bar instead of using the UITabBar. For that I used this tutorial https://guides.codepath.com/ios/Creating-a-Custom-Tab-Bar and everything is working well when my child view controllers are together with the parent vc in the same storyboard (see screenshot: same storyboard)

But I want to have just the main view controller in the main storyboard and the children in their own storyboards because there will be a lot more view controllers later, so everything is more organized.

The problem now is: it seams like the constraints are destroyed or something else (?). The interface is not built correctly anymore like you can see in this screenshot: different storyboards

I would be glad if someone can help me :)

    @IBOutlet weak var contentView: UIView!
    @IBOutlet weak var fancyTabView: UIView!
    @IBOutlet var fancyTabButtons: [UIButton]!

    var infoViewController: UIViewController!
    var challengesViewController: UIViewController!
    var meViewController: UIViewController!
    var viewControllers: [UIViewController]!

    var selectedIndex = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        infoViewController = UIStoryboard(name: "Info", bundle: nil).instantiateViewController(withIdentifier: "InfoViewController")
        challengesViewController = UIStoryboard(name: "Challenges", bundle: nil).instantiateViewController(withIdentifier: "ChallengesViewController")
        meViewController = UIStoryboard(name: "Me", bundle: nil).instantiateViewController(withIdentifier: "MeViewController")
        viewControllers = [infoViewController, challengesViewController, meViewController]

        // default selected tab button
        fancyTabButtons[selectedIndex].isSelected = true
        didPressTab(fancyTabButtons[selectedIndex])

        configureUI()
    }

    private func configureUI() {
        fancyTabView.layer.cornerRadius = 24
        fancyTabView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        fancyTabView.layer.shadowColor = UIColor.greyscaleShadow.cgColor
        fancyTabView.layer.shadowOffset = CGSize(width: 0, height: -10)
        fancyTabView.layer.shadowRadius = 40
        fancyTabView.layer.shadowOpacity = 1
    }

    @IBAction func didPressTab(_ sender: UIButton) {
        let previousIndex = selectedIndex
        selectedIndex = sender.tag
        fancyTabButtons[previousIndex].isSelected = false

        let previousVC = viewControllers[previousIndex]
        previousVC.willMove(toParent: nil)
        previousVC.view.removeFromSuperview()
        previousVC.removeFromParent()

        sender.isSelected = true
        let vc = viewControllers[selectedIndex]
        addChild(vc)
        vc.view.frame = contentView.bounds
        contentView.addSubview(vc.view)
        vc.didMove(toParent: self)
    }

Everything is working well when the view controllers are together in the same storyboard (with this code)

infoViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "InfoViewController")
challengesViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChallengesViewController")
meViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MeViewController")

but when there are in different storyboards with this code, it's not working correctly

infoViewController = UIStoryboard(name: "Info", bundle: nil).instantiateViewController(withIdentifier: "InfoViewController")
challengesViewController = UIStoryboard(name: "Challenges", bundle: nil).instantiateViewController(withIdentifier: "ChallengesViewController")
meViewController = UIStoryboard(name: "Me", bundle: nil).instantiateViewController(withIdentifier: "MeViewController")
0

There are 0 best solutions below