/// 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...