swift-UITabBarController 设置选中和未选中状态文字图片颜色或使用原图

来源:互联网 发布:网络麻将换牌软件赌博 编辑:程序博客网 时间:2024/06/15 00:57
1,对于底层框架来讲最好还是使用代码编写,虽然 storyboard 提供很大的便捷,但是 storyboard 中还是有部分功能没有,(拿最简单的设置选中的 item 颜色)
用代码实现,非常简单,一般来讲只要不需要 tabbar 非常个性化,系统的就完全满足,如果你钟情于自定义,那么自己慢慢研究吧,也并不困难
在AppDelegate中
self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
self.window?.makeKeyAndVisible()
let tabbar  =IHGTabBarController()
self.window?.rootViewController=tabbar

新建一个类:
class IHGTabBarController:UITabBarController 
//MARK:----------设置 MainTabBar
   func xzSetMainTabBar(){
       
// ---创建 viewControllers
       let xiaofeiVC =IHGXiaoFeiVC()
       
let huangouVC =IHGHuanGouVC()
       
let linggouVC =IHGLingGouVC()
       
let butieVC   =IHGBuTieVC()
       
let wodeVC    =IHGWoDeVC()
       
// ----创建 navigation
       let navigation0 =UINavigationController(rootViewController:xiaofeiVC)
       
let navigation1 =UINavigationController(rootViewController:huangouVC)
       
let navigation2 =UINavigationController(rootViewController:linggouVC)
       
let navigation3 =UINavigationController(rootViewController:butieVC)
       
let navigation4 =UINavigationController(rootViewController:wodeVC)
       
let tabArray = [navigation0,navigation1,navigation2,navigation3,navigation4]
       
//let tabArray = [xiaofeiVC,huangouVC,linggouVC,butieVC,wodeVC]
       self.viewControllers = tabArray
       
       
        navigation0.
tabBarItem.title = "0"
        navigation0.
tabBarItem.image = UIImage(named:"tabbarN0")
       
        navigation1.
tabBarItem.title = "1"
        navigation1.
tabBarItem.image = UIImage(named:"tabbarN1")
       
        navigation2.
tabBarItem.title = "2"
        navigation2.
tabBarItem.image = UIImage(named:"tabbarN2")
       
        navigation3.
tabBarItem.title = "3"
        navigation3.
tabBarItem.image = UIImage(named:"tabbarN3")
       
        navigation4.
tabBarItem.title = "4"
        navigation4.
tabBarItem.image = UIImage(named:"tabbarN4")
       

       self.tabBar.barTintColor = UIColor.whiteColor() //背景
       
self.tabBar.tintColor = UIColor.orangeColor()   //选中颜色
        

       
// ---定义未选中选中文字颜色
        UITabBarItem.appearance().setTitleTextAttributes(
            [
NSForegroundColorAttributeName:UIColor.blackColor()], forState:.Normal)
       
UITabBarItem.appearance().setTitleTextAttributes(
            [
NSForegroundColorAttributeName:UIColor.orangeColor()], forState:.Selected)
       
// --- 未选中使用原图 保持原图风格
        navigation0.tabBarItem.image = navigation0.tabBarItem.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        navigation1.
tabBarItem.image = navigation1.tabBarItem.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        navigation2.
tabBarItem.image = navigation2.tabBarItem.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        navigation3.
tabBarItem.image = navigation3.tabBarItem.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        navigation4.
tabBarItem.image = navigation4.tabBarItem.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
       
// ---- 如果你想使用未选中和选中都是自己的图片那么用下面的方法
//        navigation3.tabBarItem = UITabBarItem(title: "
3", image: UIImage(named: "tabbarN3")?.imageWithRenderingMode(.AlwaysOriginal),
//            selectedImage: UIImage(named: "tabbarY3")?.imageWithRenderingMode(.AlwaysOriginal))

       
    }

VC中的跳转
let vc =IHGShopMallVC()        
vc.hidesBottomBarWhenPushed =true  
self.navigationController?.pushViewController(vc, animated: true)

如果在 vc 中需要跳转到  另一个 TabBarItem,在按钮相应事件中:
self.navigationController?.tabBarController?.selectedIndex = 2


1 0
原创粉丝点击