swift-UITabBarController 设置选中和未选中状态文字图片颜色或使用原图
2016-03-29 12:48
453 查看
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
用代码实现,非常简单,一般来讲只要不需要 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
相关文章推荐
- Swift--map函数浅析
- Swift - String与NSString的区别,以及各自的使用场景
- Swift - 数字格式化转成字符串(保留两位小数,前面补0等)
- swift 语言的编程范式
- Swift 实例方法&类型方法(九)
- Swift里通知的使用NSNotificationCenter
- Swift解析Json返回值为null的问题
- 使用Swift打造动态库SDK和DemoAPP时所遇到的(Xcode7.3)
- 仿QQ侧滑边栏
- 关于Swift中出现dyld: Library not loaded: @rpath/libswiftCore.dylib错误的解决方法
- swift:用UIWebView读取本地html文件
- Swift -- !和?的使用
- swift系统学习控件篇:UITableView+UICollectionView
- Swift学习
- 【swift_3】swift之UITableView和UINavigation视图控制器
- swift 之?和!
- 【swift_2】swift之三大控件 UILabel、UITextField、UIButton 的使用
- 【swift_1】swift基本语法及事例Demo
- Swift 2.2 的新特性
- Swift 2.2 的新特性