swift里的UITabBarController

第一种简单的(就在AppDelegate里的)

        let vc1 = ViewController()
        let nav1 = UINavigationController(rootViewController: vc1)
        nav1.tabBarItem = UITabBarItem(title: "", image: UIImage(named: ""), selectedImage: UIImage(named: ""))
       
        let vc2 = erViewController()
        let nav2 = UINavigationController(rootViewController: vc2)
        nav2.tabBarItem = UITabBarItem(title: "", image: UIImage(named: ""), selectedImage: UIImage(named: ""))
       
       
       
        let tbv = UITabBarController()
        tbv.viewControllers = [nav1,nav2]
        //tbv.selectedViewController = nav2
        self.window?.rootViewController = tbv





第二种麻烦的

第一步

创建MainViewController(UITabBarController)、FirstViewController、SecondViewController、ThirdViewController、FourthViewController、AddViewController六个类,其中MainViewController继承于UITabBarController,AddViewController可以是ViewController也可以是个View。


AppDelegate里

self.window?.rootViewController = mainViewController()


mainViewController里

viewDidLoad()里:

self.addChildVC(childVC: firstViewController(), childTitle: "First", imageName: "xiaoXi", selectedImageName: "xiaoXi_H")

        self.addChildVC(childVC: secondViewController(), childTitle: "Second", imageName: "", selectedImageName: "")

        self.addChildVC(childVC: thirdViewController(), childTitle: "third", imageName: "", selectedImageName: "")



/// 添加子控制器

    private func addChildVC(childVC: UIViewController, childTitle: String, imageName: String, selectedImageName:String)

    {

        let navigation = UINavigationController(rootViewController: childVC)

        navigation.navigationBar.tintColor = UIColor.white //item 字体颜色

        navigation.navigationBar.barTintColor = UIColor.yellow //背景颜色

        childVC.title = childTitle

        childVC.tabBarItem.tag = 1

        childVC.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)

        childVC.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.withRenderingMode(.alwaysOriginal)

        self.addChildViewController(navigation)

    }


swift里的UITabBarController_第1张图片
mian里的

最后是这样:


swift里的UITabBarController_第2张图片

你可能感兴趣的:(swift里的UITabBarController)