Search code examples
iosswiftcarbonkit

Add a view controller as a subview in another view controller


I have already read this LINK , but not working for me. I want to show a viewController as a subview in another viewController.

Here is my code -

import UIKit
import CarbonKit

class ViewController: UIViewController, CarbonTabSwipeNavigationDelegate {

 @IBOutlet weak var containerView: UIView!

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let items = ["All",  "WOMEN",  "MEN",  "KIDS",  "HOME",  "CITY"]
        let carbonTabSwipeNavigation = CarbonTabSwipeNavigation(items: items, delegate: self)
        carbonTabSwipeNavigation.insert(intoRootViewController: self)
    }

    func carbonTabSwipeNavigation(_ carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAt index: UInt) -> UIViewController {

        //           let screen = self.storyboard?.instantiateViewController(withIdentifier: "demo") as! demo
        //           showSubViewContrller(subViewController: vc)
        //           return screen

        let storyBoard = getStoryBoardByIndentifier(identifier: "All")
        let vc = storyBoard.instantiateViewController(withIdentifier: "AllViewController") as! AllViewController
        showSubViewContrller(subViewController: vc)
        return vc
    }

    //Subview Controller
    func showSubViewContrller(subViewController:UIViewController) {
        self.addChildViewController(subViewController)
        subViewController.view.frame = containerView.frame
        self.containerView.addSubview(subViewController.view)
        subViewController.didMove(toParentViewController: self)
    }

    func getStoryBoardByIndentifier(identifier:String)->UIStoryboard {
        return  UIStoryboard.init(name: identifier, bundle: nil)
    }

}

I have a NavigationBar and a tapBar. Would like to show the viewController inside the view in a container.

enter image description here

But when the view loads it's coverUp/hide the tabBar.

enter image description here enter image description here

How to solve this and show the viewController in my specified container. Project Link - GitHub


Solution

  • Somehow i am able to fix your issue with below changes:

    Replace this method carbonTabSwipeNavigation.insert(intoRootViewController: self) with carbonTabSwipeNavigation.insert(intoRootViewController: self, andTargetView: containerView) in viewDidLoad

    Note : Give UITaBar bottom constraint to SuperView not SafeArea:

    enter image description here

    Add below code in ViewController:

    override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
    
            tabbar.invalidateIntrinsicContentSize()
        }
    

    After doing this when you run you will UITabBar:

    enter image description here