方法一:
直接在 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 = tabBarCtl
return 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
网友评论