出于纯文字代码不够直观的考虑,每种方法都附上图片,方便转换吸收。
方法一: 使用字典创建
extension MainViewController {
// 设置所有子控制器
fileprivate func setupChildControllers() {
let array = [
["className": "HomeViewController", "title": "首页", "imageName":"TabBar_home_23x23_"],
["className": "MessageViewController", "title": "消息", "imageName":"TabBar_message_23x23_"],
["className": "DiscoverViewController", "title": "发现", "imageName":"TabBar_discover_23x23_"],
["className": "MeViewController", "title": "我", "imageName":"TabBar_me_23x23_"]]
//创建视图数组
var arrayM = [UIViewController]()
for dict in array {
arrayM.append(controllerDict(dict: dict))
}
viewControllers = arrayM
}
/// 使用字典创建单个控制器
/// - 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: "发现", imageName: "TabBar_discover_23x23_")
addChildViewController(childControllerName: "MeViewController", title: "我", imageName:
"TabBar_me_23x23_")
}
// 创建单个控制器
fileprivate func addChildViewController(childControllerName: String, title: String, imageName: String) {
// 动态获取命名空间
// 将字符串转化为类,默认情况下命名空间就是项目名称,但是命名空间可以修改
guard let ns = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String,
let cls = NSClassFromString(ns + "." + childControllerName) as? UIViewController.Type
else {
return
}
// 初始化控制器
let vc = cls.init()
// 设置对应的数据
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: "发现", imageName: "TabBar_discover_23x23_")
addChildViewController(vc: MeViewController(), title: "我", imageName: "TabBar_me_23x23_")
}
// 创建单个控制器
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)
}
}