学习一门语言如何快速上手?当然是动手啦!本文教你怎样使用纯代码搭建项目前期的基本框架。自己写一个小项目,练练Swift吧!
-
首先, 需要创建一个工程。
-
所谓工欲善其事,必先利其器,我们做一下前期准备:
-
1.1 我们创建两个控制器,command+N
image.png
-
1.2 我们创建一个工具类用于获取UITabBarController
-
class ZTabBarConfigure: NSObject {
func configureZTabBarController() -> UITabBarController {
let zTabBarController: UITabBarController = UITabBarController()
let v1 = OneViewController()
v1.title = "Home"
v1.tabBarItem = UITabBarItem (title: "Home", image: UIImage(named: "home"), selectedImage: UIImage(named: "home"))
let nav1: UINavigationController = UINavigationController(rootViewController: v1)
let v2 = TwoViewController()
v2.title = "Mine"
v2.tabBarItem = UITabBarItem.init(title: "Mine", image: UIImage.init(named: "Mine"), selectedImage: UIImage.init(named: "Mine"))
let nav2: UINavigationController = UINavigationController(rootViewController: v2)
let tabArr = [nav1,nav2]
zTabBarController.viewControllers = tabArr
return zTabBarController
}
}
- 1.3 找到项目的AppDelegate.swift, 添加一个方法:
func setupWindow() {
window = UIWindow.init(frame: UIScreen.main.bounds)
window?.backgroundColor = UIColor.white
window?.makeKeyAndVisible()
let zTabBarConfigure: ZTabBarConfigure = ZTabBarConfigure()
let zTabBarController: UITabBarController = zTabBarConfigure.configureZTabBarController()
window?.rootViewController = zTabBarController
UITabBar.appearance().tintColor = UIColor.red
}
- 1.4 调用这个方法告诉编译器你的APP入口
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
setupWindow()
return true
}
OK,大功告成,模拟器上跑一下,没毛病。

网友评论