2018-06-12

swift 创建TabbarController

先上个图:

首先: 使用代码 创建页面,没有使用nib 文件:


在AppDelegate 中:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {        // Override point for customization after application launch.               

 window = UIWindow(frame: UIScreen.main.bounds)        window?.rootViewController = TabBarController()        window?.backgroundColor = UIColor.white        window?.makeKeyAndVisible()                return true    }

然后新建 tabbarController 类:

funccreatSubTabbarController() -> () {

        lethomepageVC =HomePageController()

        homepageVC.title="首页"


        letcarefulChoseVC =CarefulChoseController()

        carefulChoseVC.title="精选"


        letmineVc =MinePageController()

        mineVc.title="我的"


        letmoreVc =MoreController()

        moreVc.title="更多"


        letvcControllers = [homepageVC,carefulChoseVC,mineVc,moreVc]

        lettabbarTitle = ["首页","理财专区","我的","更多"]

        lettabbarImages = ["首页","理财专区","我的","更多"]

        letselectTabbarImages = ["首页_h","理财专区_h","我的_h","更多_h"]


        vartabControllers :[UINavigationController] = []



        for(index,value)invcControllers.enumerated() {

            lettitle:String= tabbarTitle[index]

            letimage:String= tabbarImages[index]

            letselectImage:String= selectTabbarImages[index]


            letnav =UINavigationController.init(rootViewController: value)

            nav.tabBarItem.title= title

            nav.tabBarItem.image=UIImage.init(named: image)

            nav.tabBarItem.selectedImage=UIImage.init(named: selectImage)

            value.navigationController?.isNavigationBarHidden = true

            tabControllers.append(nav)

        }

        viewControllers= tabControllers

    }

你可能感兴趣的:(2018-06-12)