import UIKit
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.backgroundColor = UIColor.white
self.tabBar.isOpaque = false
setUpTabBar(img: "首页", selImg: "首页Sel", title: "预警", viewController: MainViewController())
setUpTabBar(img: "我的", selImg: "我的Sel", title: "我的", viewController: MineViewController())
}
func setUpTabBar(img:String, selImg:String, title:String, viewController:BasicViewController) {
let nav = UINavigationController(rootViewController: viewController)
nav.isNavigationBarHidden = true
nav.tabBarItem.image = UIImage(named: img)?.withRenderingMode(.alwaysOriginal)
nav.tabBarItem.selectedImage = UIImage(named: selImg)?.withRenderingMode(.alwaysOriginal)
nav.title = title
nav.tabBarItem.setTitleTextAttributes([.foregroundColor: UIColor(red: 165.0/255.0, green: 165.0/255.0, blue: 165.0/255.0, alpha: 1), .font: UIFont.systemFont(ofSize: 12)], for: UIControlState.normal)
nav.tabBarItem.setTitleTextAttributes([.foregroundColor: ButtonColor, .font: UIFont.systemFont(ofSize: 12)], for: UIControlState.selected)
self.addChildViewController(nav)
}
}
使用:
let appDelegate = (UIApplication.shared.delegate) as! AppDelegate
appDelegate.window!.rootViewController = TabBarViewController()