swift 通过plist文件初始化tabbar

使用plist文件来加载控制器以及图片文字等资源

func setup() {
        UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red:56/255.0, green:165/255.0, blue:241/255.0, alpha:1)], forState: .Selected)
        UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red:132/255.0, green:132/255.0, blue:132/255.0, alpha:1)], forState: .Normal)
        self.tabBar.backgroundImage = UIImage(named: "tabbar_back")
        
        let path = NSBundle.mainBundle().pathForResource("YWTabBarViewController", ofType: "plist")
        let array = NSArray(contentsOfFile: path!)

        if array != nil {
            for dic in array! {
                //将类名转化为类
                //1.获取命名空间
                let clsName = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
                
                let forDic = dic as! NSDictionary
                //2.把字符串形式的类名称转换成类
                let classType = NSClassFromString(clsName + "." + "\(forDic["viewController"]!)") as! UIViewController.Type
                //3.通过class创建对象
                let vc = classType.init()
     
                vc.tabBarItem.image = UIImage(named: "\(forDic["image"]!)")
                vc.tabBarItem.selectedImage = UIImage(named: "\(forDic["selectImage"]!)")
                vc.tabBarItem.title = "\(forDic["title"]!)"
                addChildViewController(vc)
            }
        }

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,774评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,027评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 每一段情深意长的背后总有一个人在默默坚持,而每一段情深意长的结局不...
    守素阅读 425评论 0 2
  • 表弟明天结婚,今天相当于前期的准备,亲朋好友都相聚在一起,非常之热闹,男人或打扑克,或麻将,女人们则帮忙贴窗花...
    赵程冲阅读 315评论 0 0