import UIKit
class MainTabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// 1.初始化所有的子控制器
setUpChildViewController()
}
}
// MARK: - setUpChildViewController
extension MainTabBarViewController {
fileprivate func setUpChildViewController() {
let homeVC = TabBar1ViewController()
setUpNavRootViewControllers(vc: homeVC, title: "首页", image: "home_icon", selImage: "home_icon1")
let musicVC = TabBar2ViewController()
setUpNavRootViewControllers(vc: musicVC, title: "音乐", image: "yuershi_icon", selImage: "yuershi_icon1")
let discoverVC = TabBar3ViewController()
setUpNavRootViewControllers(vc: discoverVC, title: "发现", image: "kefu_icon", selImage: "kefu_icon1")
let mineVC = TabBar4ViewController()
setUpNavRootViewControllers(vc: mineVC, title: "我的", image: "wode_icon", selImage: "wode_icon1")
}
// 初始化一个到导航控制器的控制器
fileprivate func setUpNavRootViewControllers(vc: UIViewController, title:String, image: String, selImage: String) {
vc.title = title
vc.tabBarItem.selectedImage = UIImage(named: selImage)!.withRenderingMode(.alwaysOriginal)
vc.tabBarItem.image = UIImage(named: image)!.withRenderingMode(.alwaysOriginal)
vc.tabBarItem.setTitleTextAttributes(NSDictionary(object:UIColor (colorLiteralRed: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1), forKey:NSForegroundColorAttributeName as NSCopying) as? [String : AnyObject], for:UIControlState.normal);
vc.tabBarItem.setTitleTextAttributes(NSDictionary(object:UIColor.red, forKey:NSForegroundColorAttributeName as NSCopying) as? [String : AnyObject], for:UIControlState.selected)
// tabbar 背景颜色
// UITabBar.appearance().tintColor=UIColor.white
// UITabBar.appearance().isTranslucent=false
// 这里用了一个自定义的 NavigationController
self.addChildViewController(BaseNavigationController(rootViewController: vc))
}
}
如有问题欢迎指正,谢谢。