Swift_使用命名空间创建

import UIKit

class MainTableViewController: UITabBarController {

    

    override func viewDidLoad() {

        super.viewDidLoad()

        tabBar.tintColor = UIColor.orange

        

        guard let filePath = Bundle.main.path(forResource: "MainVCSettings", ofType: "json") else {

            NSLog(message: "数据有误")

            return

        }

        guard let data = NSData.init(contentsOfFile: filePath) else {

            return

        }

        do {

            let  objc = try

                JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String : AnyObject]]

            for dict in objc {

                let  title = dict["title"] as? String

                let  vcName = dict["vcName"] as? String

                let  imageName  = dict["imageName"] as? String

                addChildViewController(vcName, titleName: title, imageName: imageName)

            }

        } catch  {

            addChildViewController("HomeTableViewController", titleName: "首页", imageName: "tabbar_home")

             addChildViewController("MessageTableViewController", titleName: "消息", imageName: "tabbar_message_center")

             addChildViewController("DiscoverTableViewController", titleName: "发现", imageName: "tabbar_discover")

             addChildViewController("ProfileTableViewController", titleName: "我", imageName: "tabbar_profile")

        }

        

    }

    

    func addChildViewController(_ childControllerName: String?,titleName: String?,imageName:String?){

        

        guard let name = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {

            NSLog(message: "获取命名空间失败")

            return

        }

        

        

        var cls:AnyClass? = nil

        if let vcName = childControllerName {

            cls = NSClassFromString(name + "." + vcName)

        }

        

        

        guard let typeCls = cls as? UITableViewController.Type else{

            return

        }

        

        let childController = typeCls.init()

        addChildViewControllerClass(childController, titleName: titleName, imageName: imageName)

    }

    //添加一个子控制器

    func addChildViewControllerClass(_ childController: UITableViewController,titleName: String?,imageName:String?) {

          //设置自控制器相关属性

//        childController.tabBarItem.title = titleName

//         childController.navigationItem.title = titleName

         //该方法会由内向外的设置标题

        

        if let ivName = imageName {

            childController.tabBarItem.image = UIImage.init(named: ivName)

            childController.tabBarItem.selectedImage = UIImage.init(named: "\(ivName)_highlighted")

        }

        if let navTitle = titleName {

              childController.title = navTitle

        }

         //包装一个导航控制器

        let nav = UINavigationController.init(rootViewController: childController)

         //设置导航栏的背景颜色

         nav.navigationBar.barTintColor = UIColor.lightGray

        //标题颜色

         let dic = [NSAttributedString.Key.foregroundColor : UIColor.orange,NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 18)]

        nav.navigationBar.titleTextAttributes = dic

         //item颜色

         nav.navigationBar.tintColor = UIColor.gray

        addChild(nav)

    }

}

前边     if let name = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String {

中的name就是这里设置的

如果再加上服务器网络请求可以做成动态的实现apptabbar展示页等。但是这些页面必须提前做好放到项目里面

我在本地添加了一个.json文件

猜你喜欢

转载自blog.csdn.net/NoPolun_iOS/article/details/84788967