/// TabBarItem配置信息
fileprivate struct TabBarItemInfo {
var vc: UIViewController
var title: String
var normalImgName: String
var selectedImgName: String
}
class TabbarController: UITabBarController {
fileprivate let vcInfos = [TabBarItemInfo(vc: HomeVC(),
title: "首页",
normalImgName: "main_tabbar_wifi_normal",
selectedImgName: "main_tabbar_wifi_selected"),
TabBarItemInfo(vc: MineVC(),
title: "我的",
normalImgName: "main_tabbar_speed_test_normal",
selectedImgName: "main_tabbar_speed_test_selected"),
TabBarItemInfo(vc: SettingVC(),
title: "设置",
normalImgName: "main_tabbar_setting_normal",
selectedImgName: "main_tabbar_setting_selected")]
override func viewDidLoad() {
super.viewDidLoad()
// 设置不透明
self.tabBar.isTranslucent = false
// tabBar背景颜色
self.tabBar.barTintColor = UIColor.white
// 隐藏tabBar分割线
// self.tabBar.backgroundImage = UIImage()
// self.tabBar.shadowImage = UIImage()
// 设置子控制器
self.viewControllers = self.setupViewControllers()
}
/// 设置ViewControllers的TabBarItem
fileprivate func setupViewControllers() -> [UIViewController] {
var vcs = [UIViewController]()
for info in vcInfos {
let vc = NavigationController(rootViewController: info.vc)
vcs.append(vc)
vc.tabBarItem.title = info.title
vc.tabBarItem.image = UIImage(named: info.normalImgName)?.withRenderingMode(.alwaysOriginal)
vc.tabBarItem.selectedImage = UIImage(named: info.selectedImgName)?.withRenderingMode(.alwaysOriginal)
vc.tabBarItem.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10),
NSAttributedString.Key.foregroundColor: UIColor.black],
for: UIControl.State.normal)
vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10),
NSAttributedString.Key.foregroundColor: UIColor.red],
for: UIControl.State.selected)
}
return vcs
}
}
UITabBarController
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1. UITabBarItem 显示正常的图标 我们使用 UITabBarController 都会这么写 但是出...
- //创建UITabbarController -- 标签视图控制器 UITabBarController *tab...
- 很多人在使用UINavigationController和UITabBarController时候对设置根视图不知...
- 使用步骤: 1.初始化UITabBarController2.设置UIWindow的rootViewControl...