如何使用标签栏管理Container View中的控制器

时间:2018-03-22 09:07:15

标签: ios swift uinavigationcontroller uitabbar uicontainerview

在我的故事板上,我有主ViewController,而不是TabBarViewController,它由底部的TabBar,顶部的视图和中间的ContainerView组成。 ContainerView有一个NavigationController。我还有4 ViewControllers,其中一个是RootViewController NavigationController。我希望在选择TabBarItem时显示ViewControllers之一,并且将来我会添加幻灯片菜单,它也会显示所选的ViewController。
我有下一个代码,它只显示ContainerView中的初始ViewController,当我选择TabBarItems时,新的ViewControllers没有显示,我只看到第一个View Controller。出了什么问题?

class ViewController: UIViewController {

    @IBOutlet weak var container: UIView!
    @IBOutlet weak var first: UITabBarItem!
    @IBOutlet weak var second: UITabBarItem!
    @IBOutlet weak var third: UITabBarItem!
    @IBOutlet weak var fours: UITabBarItem!
    @IBOutlet weak var tabBar: UITabBar!

    var firstVC: FirstViewController?
    var secondVC: SecondViewController?
    var thirdVC: ThirdViewController?
    var foursVC: FoursViewController?

    var navi: UINavigationController?




    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.delegate = self
        initialSetup()
    }

    func initialSetup() {

        tabBar.selectedItem = tabBar.items?.first

        navi = self.storyboard?.instantiateViewController(withIdentifier: "containerNavi") as? UINavigationController

        firstVC = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as? FirstViewController
        secondVC = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
        thirdVC = self.storyboard?.instantiateViewController(withIdentifier: "ThirdViewController") as? ThirdViewController
        foursVC = self.storyboard?.instantiateViewController(withIdentifier: "FoursViewController") as? FoursViewController
    }

    func showVC(number: Int) {
        switch number {
        case 0:
            navi?.popToRootViewController(animated: true)
            print("0")
        case 1:
            if let second = secondVC {
                navi?.pushViewController(second, animated: true)
            }
            print("1")
        case 2:
            if let third = thirdVC {
                navi?.pushViewController(third, animated: true)
            }
            print("2")
        case 3:
            if let fours = foursVC {
                navi?.pushViewController(fours, animated: true)
            }
            print("3")
        default:
            return
        }
    }


}

extension ViewController: UITabBarDelegate {

    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        showVC(number: item.tag)
    }

}

故事板截图:ScreenShot

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此extesion添加/删除4到containerView

中的任何一个
extension UIViewController {
    func add(_ child: UIViewController, frame: CGRect? = nil) {
        addChildViewController(child)
        if let frame = frame {
            child.view.frame = frame
        }
        view.addSubview(child.view)
        child.didMove(toParentViewController: self)
    }
    func remove() {
        willMove(toParentViewController: nil)
        view.removeFromSuperview()
        removeFromParentViewController()

   }

}

//像这样使用

let vc = self.storyboard?.instantiateViewController(withIdentifier: "first")

self.add(vc, frame: self.containerView.frame)

删除

vc.remove()