首页 > 代码库 > 一.界面整体框架搭建
一.界面整体框架搭建
实现的界面截图如下:
项目的目录结构如下:
实现的代码如下:
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 }}
一.界面整体框架搭建
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。