美文网首页iOS开发精华专题
Swift3.0 几行代码简单框架的搭建

Swift3.0 几行代码简单框架的搭建

作者: Courage_SC | 来源:发表于2017-07-05 10:23 被阅读12次
    图.png

    AppDelegate里的代码

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            //1. 创建window
            window = UIWindow(frame: UIScreen.main.bounds)
            window?.backgroundColor = UIColor.white
            window?.rootViewController = MainViewController()
            //2. 显示window
            window?.makeKeyAndVisible()
            return true
        }
    

    MainViewController.swift

    import UIKit
    
    class MainViewController: UITabBarController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            addChildViewControllers()
        }
    
        /**
         添加所有子控制
         */
        func addChildViewControllers() {
            addChildViewController(childController: HomeTableViewController(), title: "首页", imageName: "tabbar_home")
            addChildViewController(childController: MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
            addChildViewController(childController: DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
            addChildViewController(childController: ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        }
        
        /**
         初始化子控制器
         
         :param: childController 需要初始化的子控制器
         :param: title           初始化的标题
         :param: imageName       初始化的图片
         */
        func addChildViewController(childController: UIViewController, title:String, imageName:String) {
            // 从内像外设置, nav和tabbar都有
            childController.title = title
            childController.tabBarItem.image = UIImage(named: imageName)
            childController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
            
            // 注意: Xocde7之前只有文字有效果, 还需要设置图片渲染模式
            tabBar.tintColor = UIColor.orange
            
            // 2.创建导航控制器
            let nav = UINavigationController()
            nav.addChildViewController(childController)
            
            // 3.添加控制器到tabbarVC
            addChildViewController(nav)
        }
    }
    
    

    相关文章

      网友评论

        本文标题:Swift3.0 几行代码简单框架的搭建

        本文链接:https://www.haomeiwen.com/subject/ekgfhxtx.html