本项目假设是经典结构,即下面一个tabbar,上面几个navigationbar,采用纯代码开发。
1. 用Xcode创建一个项目,选择语言Swift
2. 创建一个桥接文件(Header File),可以和OC混编
还要进行一些配置
3. 添加一个Config文件,相当于OC当中的宏文件
config.swift里面代码参考:
let kScreenWidth = UIScreen.main.bounds.width
let kScreenHeight = UIScreen.main.bounds.height
let kScreenScale: CGFloat = UIScreen.main.scale
let kAppVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
// 适配界面相关
let kNavBarHeight: CGFloat = 44.0
let kStatusBarHeight: CGFloat = UIDevice.isIphoneX() ? 44.0 : 20.0
let kStatusAddHeight: CGFloat = kStatusBarHeight - 20.0
let kTopHeight: CGFloat = kStatusBarHeight + kNavBarHeight
let kTabBarHeight: CGFloat = UIDevice.isIphoneX() ? 83.0 : 49.0
let kTabBarAddHeight: CGFloat = kTabBarHeight - 49.0
4. 因为我们采用纯代码,所以删掉一些文件
将ViewController.swift和Main.storyboard删掉
把这里配置删除
5. 启动的代理函数里面写一些界面入口
代码参考如下:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))
let tabbarController = UITabBarController()
let rankController = UINavigationController(rootViewController: RankViewController())
let searchController = UINavigationController(rootViewController: SearchViewController())
let pushController = UINavigationController(rootViewController: PushViewController())
let moreController = UINavigationController(rootViewController: MoreViewController())
tabbarController.viewControllers = [rankController, searchController, pushController, moreController];
let tabbarItem1 = UITabBarItem(title: "排行榜", image: UIImage(named: "bio"), selectedImage: UIImage(named: "bio_red"))
let tabbarItem2 = UITabBarItem(title: "搜索", image: UIImage(named: "bio"), selectedImage: UIImage(named: "bio_red"))
let tabbarItem3 = UITabBarItem(title: "圈子", image: UIImage(named: "bio"), selectedImage: UIImage(named: "bio_red"))
let tabbarItem4 = UITabBarItem(title: "更多", image: UIImage(named: "bio"), selectedImage: UIImage(named: "bio_red"))
rankController.tabBarItem = tabbarItem1
searchController.tabBarItem = tabbarItem2
pushController.tabBarItem = tabbarItem3
moreController.tabBarItem = tabbarItem4
rankController.tabBarController?.tabBar.tintColor = MAIN_RED
self.window?.rootViewController = tabbarController
self.window?.makeKeyAndVisible()
return true
}