Swift|Swift 4.0 如何改变 tabBar 标签栏图片、字体颜色和大小
方法一:
直接在 AppDelegate 的didFinishLaunchingWithOptions 方法中设置:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {let nav1 = UINavigationController(rootViewController: UIViewController())
nav1.tabBarItem = UITabBarItem(title: "首页", image: UIImage(named: "tabbar_normal1"), selectedImage: UIImage(named: "tabbar_selected1"))let nav2 =UINavigationController(rootViewController: UIViewController())
nav2.tabBarItem = UITabBarItem(title: "消息", image: UIImage(named: "tabbar_normal2"), selectedImage: UIImage(named: "tabbar_selected2"))let nav3 = UINavigationController(rootViewController: UIViewController())
nav3.tabBarItem = UITabBarItem(title: "我的", image: UIImage(named: "tabbar_normal3"), selectedImage: UIImage(named: "tabbar_selected3"))let nav4 = UINavigationController(rootViewController: UIViewController())
nav4.tabBarItem = UITabBarItem(title: "更多", image: UIImage(named: "tabbar_normal4"), selectedImage: UIImage(named: "tabbar_selected4"))let tabBarCtl = UITabBarController()
tabBarCtl.viewControllers = [nav1, nav2, nav3, nav4]for item in tabBarCtl.viewControllers! {
// 遵循图片原始颜色,否则图片会变成系统默认的蓝色
item.tabBarItem.selectedImage = item.tabBarItem.selectedImage!.withRenderingMode(.alwaysOriginal)
// 修改标签栏选中时文字颜色
item.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .selected)
// 修改标签栏未选中时文字颜色
item.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.green], for: .normal)
}self.window!.rootViewController = tabBarCtlreturn true
}
方法二:
自定义 UITabBarController:
class CustomTabBarController: UITabBarController {override func viewDidLoad() {
super.viewDidLoad()// 修改标签栏的 bar 颜色
//self.tabBar.barTintColor = UIColor.white
// 修改标签栏的默认 tintColor,会改变图片和文字
//self.tabBar.tintColor = UIColor.yellow// 修改标签栏选中时文字颜色、字体
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 18.0)], for: .selected)
// 修改标签栏未选中时文字颜色、字体
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.orange, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 18.0)], for: .normal)
// 设置字体偏移
//UITabBarItem.appearance().titlePositionAdjustment = UIOffsetMake(0.0, 0.0)self.addChildVC()
}override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}/// 添加控制器
fileprivate func addChildVC() {
self.setupOneChildViewController(UIViewController(), title: "首页", normalImage: "tabbar_normal1", selectedImage: "tabbar_selected1")
self.setupOneChildViewController(UIViewController(), title: "消息", normalImage: "tabbar_normal2", selectedImage: "tabbar_selected2")
self.setupOneChildViewController(UIViewController(), title: "我的", normalImage: "tabbar_normal3", selectedImage: "tabbar_selected3")
self.setupOneChildViewController(UIViewController(), title: "更多", normalImage: "tabbar_normal4", selectedImage: "tabbar_selected4")
}fileprivate func setupOneChildViewController(_ vc: UIViewController, title: String, normalImage: String, selectedImage: String) {
let nav = UINavigationController(rootViewController: vc)
nav.tabBarItem = UITabBarItem(title: title, image: UIImage(named: normalImage), selectedImage: UIImage(named: selectedImage))
// 设置选中图片颜色,否则图片会变成系统默认的蓝色
nav.tabBarItem.selectedImage = nav.tabBarItem.selectedImage!.withRenderingMode(.alwaysOriginal)
addChildViewController(nav)
}}
文章图片
C125137D-5E64-4BAE-A444-B1479FAFF752.png 【Swift|Swift 4.0 如何改变 tabBar 标签栏图片、字体颜色和大小】参考链接
推荐阅读
- 考研英语阅读终极解决方案——阅读理解如何巧拿高分
- 如何寻找情感问答App的分析切入点
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus使用queryWrapper如何实现复杂查询
- 如何在Mac中的文件选择框中打开系统隐藏文件夹
- 漫画初学者如何学习漫画背景的透视画法(这篇教程请收藏好了!)
- java中如何实现重建二叉树
- Linux下面如何查看tomcat已经使用多少线程
- thinkphp|thinkphp 3.2 如何调用第三方类库
- 2019女表什么牌子好(如何挑选女士手表?)