天天看點

swift —Tabbar

首先建立一個繼承UITabBarController的檔案,可以在viewDidLoad直接寫

let walletVC = CHAWalletViewController()
        let walletNav = UINavigationController(rootViewController: walletVC)
        walletNav.tabBarItem.title = "錢包"
        //alwaysOriginal 圖檔保持原來的顔色
        walletNav.tabBarItem.image = UIImage(named: "tab_wallet_nor")?.withRenderingMode(.alwaysOriginal)
        walletNav.tabBarItem.selectedImage = UIImage(named: "tab_wallet_sel")?.withRenderingMode(.alwaysOriginal)
        
        let DappVC = CHADAppViewController()
        let DappNav = UINavigationController(rootViewController: DappVC)
        DappNav.title = "DApp"
        DappNav.tabBarItem.image = UIImage(named: "tab_dapp_nor")?.withRenderingMode(.alwaysOriginal)
        DappNav.tabBarItem.selectedImage = UIImage(named: "tab_dapp_sel")?.withRenderingMode(.alwaysOriginal)
        
        let infomationVC = CHAInformationViewController()
        let infomationNav = UINavigationController(rootViewController: infomationVC)
        infomationNav.title = "資訊"
        infomationNav.tabBarItem.image = UIImage(named: "tab_infomation_nor")?.withRenderingMode(.alwaysOriginal)
        infomationNav.tabBarItem.selectedImage = UIImage(named: "tab_infomation_sel")?.withRenderingMode(.alwaysOriginal)
        
        let settingVC = CHASettingViewController()
        let settingNav = UINavigationController(rootViewController: settingVC)
        settingNav.title = "設定"
        UIImage.init().withRenderingMode
        settingNav.tabBarItem.image = UIImage(named: "tab_setting_nor")?.withRenderingMode(.alwaysOriginal)
        
        settingNav.tabBarItem.selectedImage = UIImage(named: "tab_setting_sel")?.withRenderingMode(.alwaysOriginal)
        
        self.viewControllers = [walletNav,DappNav,infomationNav,settingNav
        ----------------------------------分割線----------------------------------
		//tabBar選中按鈕的顔色
        self.tabBar.tintColor = UIColor.colorWithHex(hexStr: "#93599E")
        //tabBar未選中按鈕的顔色
        self.tabBar.unselectedItemTintColor = UIColor.colorWithHex(hexStr: "#575769")
        //tabBarn背景顔色
        self.tabBar.barTintColor = UIColor.colorWithHex(hexStr: "#2D2D38")
           

也可以對分割線上面做一個簡單的封裝

func setUpChildViewController(viewController:UIViewController, title:NSString, image:UIImage, selectImage:UIImage) {
        
        let naviVC = UINavigationController(rootViewController: viewController)
        naviVC.tabBarItem = UITabBarItem(title: title as String, image: image.withRenderingMode(.alwaysOriginal), selectedImage: selectImage.withRenderingMode(.alwaysOriginal))
        self.addChild(naviVC)
    }
           

然後調用

self.setUpChildViewController(viewController: CHAWalletViewController(), title: "錢包", image: UIImage(named: "tab_wallet_nor")!, selectImage: UIImage(named: "tab_wallet_sel")!)

        self.setUpChildViewController(viewController: CHADAppViewController(), title: "DApp", image: UIImage(named: "tab_dapp_nor")!, selectImage: UIImage(named: "tab_dapp_sel")!)

        self.setUpChildViewController(viewController: CHAInformationViewController(), title: "資訊", image: UIImage(named: "tab_infomation_nor")!, selectImage: UIImage(named: "tab_infomation_sel")!)

        self.setUpChildViewController(viewController: CHASettingViewController(), title: "設定", image: UIImage(named: "tab_setting_nor")!, selectImage: UIImage(named: "tab_setting_sel")!)
        
        
        //tabBar選中按鈕的顔色
        self.tabBar.tintColor = UIColor.colorWithHex(hexStr: "#93599E")
        //tabBar未選中按鈕的顔色
        self.tabBar.unselectedItemTintColor = UIColor.colorWithHex(hexStr: "#575769")
        //tabBarn背景顔色
        self.tabBar.barTintColor = UIColor.colorWithHex(hexStr: "#2D2D38")
           

AppDelegate裡面

self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.backgroundColor = UIColor.white
        
        let tabbleVC = CHATabBarViewController()
        self.window?.rootViewController = tabbleVC
        self.window?.makeKeyAndVisible()
           

效果

swift —Tabbar

連結: tabbar.