首页 > 代码库 > 一.界面整体框架搭建

一.界面整体框架搭建

实现的界面截图如下:

技术分享

 

项目的目录结构如下:

技术分享

 

实现的代码如下:

1>

AppDelegate:

////  AppDelegate.swift//  WeiBoSwift////  Created by 思 彭 on 16/9/24.//  Copyright © 2016年 思 彭. All rights reserved.//import UIKit@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {    var window: UIWindow?    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {        // 创建窗口        window = UIWindow()        window?.backgroundColor = UIColor.white        window?.rootViewController = PSMainViewController()        window?.makeKeyAndVisible()        return true    }

2>

PSMainViewController:

////  PSMainViewController.swift//  WeiBoSwift////  Created by 思 彭 on 16/9/24.//  Copyright © 2016年 思 彭. All rights reserved.//import UIKitclass PSMainViewController: UITabBarController {    override func viewDidLoad() {        super.viewDidLoad()        setupChildControllers()    }}//MARK: 设置子控制器extension PSMainViewController{        /// 设置所有子控制器    func setupChildControllers() {                let array = [            ["clsName": "PSHomeViewController","title": "首页","imageName": "home"],            ["clsName": "PSMessageViewController","title": "消息","imageName": "message_center"],            ["clsName": "PSDiscoverViewController","title": "发现","imageName": "discover"],            ["clsName": "PSProfileViewController","title": "我的","imageName": "profile"],        ]        var arrayM = [UIViewController]()        for dict in array {            arrayM.append(controller(dict: dict as [String : AnyObject]))        }        viewControllers = arrayM    }        // 使用字典创建一个子控制器    private func controller(dict: [String : AnyObject]) ->UIViewController {                // 1. 取得字典内容 <守护>        guard let clsName = dict["clsName"] as? String,            let title = dict["title"] as? String,            let imageName = dict["imageName"] as? String,            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type            else{                return UIViewController()        }        // 2. 创建视图控制器        let vc = cls.init()        vc.title = title            // 3. 设置图像        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)                // 4. 设置 tabbar 的标题字体(大小)        vc.tabBarItem.setTitleTextAttributes(            [NSForegroundColorAttributeName: UIColor.orange],            for: .highlighted)        // 系统默认是 12 号字,修改字体大小,要设置 Normal 的字体大小        vc.tabBarItem.setTitleTextAttributes(            [NSFontAttributeName: UIFont.systemFont(ofSize: 12)],            for: UIControlState(rawValue: 0))                // 实例化导航控制器的时候,会调用 push 方法将 rootVC 压栈        let nav = UINavigationController(rootViewController: vc)        return nav    }}

 

一.界面整体框架搭建