? ? ? ? ? ? ? ?出于純文字代碼不夠直觀的考慮昔逗,每種方法都附上圖片,方便轉換吸收酱吝。
方法一: 使用字典創(chuàng)建
extension MainViewController {
// 設置所有子控制器
fileprivate func setupChildControllers() {
let array = [
["className": "HomeViewController", "title": "首頁", "imageName":"TabBar_home_23x23_"],
["className": "MessageViewController", "title": "消息", "imageName":"TabBar_message_23x23_"],
["className": "DiscoverViewController", "title": "發(fā)現(xiàn)", "imageName":"TabBar_discover_23x23_"],
["className": "MeViewController", "title": "我", "imageName":"TabBar_me_23x23_"]]
//創(chuàng)建視圖數(shù)組
var arrayM = [UIViewController]()
for dict in array {
arrayM.append(controllerDict(dict: dict))
}
viewControllers = arrayM
}
/// 使用字典創(chuàng)建單個控制器
/// - Parameter dict: 字典信息【className, title, imageName】
/// - Returns: 子控制器
fileprivate func controllerDict(dict:[String: String]) -> UIViewController {
// 獲取字典內容? 命名空間
guard let className = dict["className"],
let title = dict["title"],
let imageName = dict["imageName"],
let ns = Bundle.main.infoDictionary?["CFBundleName"] as? String,
let cls = NSClassFromString(ns + "." + className) as? UIViewController.Type
else {
return UIViewController()
}
// 初始化控制器
let vc = cls.init()
// 設置標題和圖標
vc.title = title
vc.tabBarItem.image = UIImage(named: imageName)
vc.tabBarItem.selectedImage = UIImage(named: imageName + "selected")
// 給每個控制器包裝一個導航控制器
let nav = NavigationController(rootViewController: vc)
return nav
}
}
方法二:字符串轉類別
extension MainViewController {
// 設置所有子控制器
fileprivate func addChildViewControllers() {
addChildViewController(childControllerName: "HomeViewController", title: "首頁", imageName: "TabBar_home_23x23_")
addChildViewController(childControllerName: "MessageViewController", title: "消息", imageName: "TabBar_message_23x23_")
addChildViewController(childControllerName: "DiscoverViewController", title: "發(fā)現(xiàn)", imageName: "TabBar_discover_23x23_")
addChildViewController(childControllerName: "MeViewController", title: "我", imageName:
"TabBar_me_23x23_")
}
// 創(chuàng)建單個控制器
fileprivate func addChildViewController(childControllerName: String, title: String, imageName: String) {
// 動態(tài)獲取命名空間
// 將字符串轉化為類禽车,默認情況下命名空間就是項目名稱艇搀,但是命名空間可以修改
guard let ns = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String,
let cls = NSClassFromString(ns + "." + childControllerName) as? UIViewController.Type
else {
return
}
// 初始化控制器
let vc = cls.init()
// 設置對應的數(shù)據(jù)
vc.tabBarItem.image = UIImage(named: imageName)
vc.tabBarItem.selectedImage = UIImage(named: imageName + "selected")
// 設置子控制器標題
vc.title = title
// 給每個控制器包裝一個導航控制器
let nav = NavigationController(rootViewController: vc)
addChildViewController(nav)
}
}
方法三:直接包裝視圖控制器
class MainViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
addChildViewControllers()
}
}
extension MainViewController {
// 設置所有子控制器
fileprivate func addChildViewControllers() {
addChildViewController(vc: HomeViewController(), title: "首頁", imageName: "TabBar_home_23x23_")
addChildViewController(vc: MessageViewController(), title: "消息", imageName: "TabBar_message_23x23_")
addChildViewController(vc: DiscoverViewController(), title: "發(fā)現(xiàn)", imageName: "TabBar_discover_23x23_")
addChildViewController(vc: MeViewController(), title: "我", imageName: "TabBar_me_23x23_")
}
// 創(chuàng)建單個控制器
fileprivate func addChildViewController(vc: UIViewController, title: String, imageName: String) {
// 設置子控制器標題
vc.title = title
// 設置子控制器圖標
vc.tabBarItem.image = UIImage(named: imageName)
vc.tabBarItem.selectedImage = UIImage(named: imageName + "selected") // TabBar_home_23x23_selected
// 給每個子控制器包裝一個導航控制器
let nav = NavigationController(rootViewController: vc)
addChildViewController(nav)
}
}