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() //选中颜色
// ---创建 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
- swift-UITabBarController 设置选中和未选中状态文字图片颜色或使用原图
- UITabBarController设置选中时图片和文字的颜色
- segment文字颜色,选中颜色,未选中颜色设置
- UITabBarController 之 设置选中和未选中的图片 隐藏tabbar
- UITabBarController 之 设置选中和未选中的图片 隐藏tabbar
- iOS开发 在storyboard中拖的UITabBarController,设置选中时的图片和字体颜色
- 设置tabbaritem的title颜色(选中和未选中)
- 设置tablayout选中文字颜色和背景图片
- checkbox 设置为选中和未选中状态
- JS设置checkbox 为选中和未选中状态
- iOS 设置UITabBar,背景颜色,默认图片,文字颜色,和选中颜色.
- tabbar未选中和选中时设置不同的图片
- tabBarItem 选中和未选中的图片设置
- TabBar 的selecedImage无法显示设置图片问题 和item文字颜色选中设置
- iOS开发UITabBarController设置选中图片
- UITabBar 设置选中、未选中状态下title的字体颜色
- check,checkbox 设置选中和未选中
- 使用storyboard,设置系统自带的tabbar的选中状态,未选中状态
- hadoop从安装到布署
- 经典算法——求绝对值溢出问题
- Android中的回调
- ubuntu下搭建erlang编程环境
- 机器学习基础(三十) —— 线性回归、正则化(regularized)线性回归、局部加权线性回归(LWLR)
- swift-UITabBarController 设置选中和未选中状态文字图片颜色或使用原图
- ios:画直线和虚线
- Intellij IDEA 的使用
- 百度地图开发(一)——地图定位
- 自定义UICollectionViewFlowLayout
- Redis源码解析:12AOF持久化
- java接口可以实例化吗?
- 脉络清晰的BP神经网络讲解,赞
- HDC,CDC,CClientDC的区别和联系是什么?