iOS开发基础--代码实现包含NavigationController和TabBarController的界面

现在TabBarController基本是各种应用的标配了NavigationController更是处处都要用到,如果用storyboard开发直接在上面拖界面连线即可,如果要用纯代码开发该如何写呢?我们可以分为如下几个步骤

1.在appDelegate中初始化UIWindow
2.创建TabBarController实例
3.将所需界面的rootViewController设置为NavigationController
4.将所需页面添加到tabbarController上
5.自定义各自的UITabBarItem
6.设置window的rootViewController为tabbarController,并让其显示

!!!!!注:如果要通过代码实现请把main.storyboard删掉且在info.plist中将Main storyboard file base name的Value值清空

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //初始化窗口
        self.window = UIWindow(frame: CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT))
        
        //创建TabBarController实例
        let tabbarController = UITabBarController()
        
        //将所需界面的rootViewController设置为NavigationController
        let rankController = UINavigationController(rootViewController: rankViewController())
        let searchController = UINavigationController(rootViewController: searchViewController())
        let pushController = UINavigationController(rootViewController: pushViewController())
        let circleController = UINavigationController(rootViewController: circleViewController())
        let moreController = UINavigationController(rootViewController: moreViewController())
        
        //将这五个页面添加到tabbarController上
        tabbarController.viewControllers = [rankController,searchController,pushController,circleController,moreController]
        
        //设置五个UITabBarItem
        let tabbarItem1 = UITabBarItem(title: "排行榜", image: UIImage(named: "bio"), selectedImage: UIImage(named: "bio_red"))
        let tabbarItem2 = UITabBarItem(title: "发现", image: UIImage(named: "timer 2"), selectedImage: UIImage(named: "timer 2"))
        let tabbarItem3 = UITabBarItem(title: "", image: UIImage(named: "pencil"), selectedImage: UIImage(named: "user two-2_red"))
        let tabbarItem4 = UITabBarItem(title: "圈子", image: UIImage(named: "users two-2"), selectedImage: UIImage(named: "users two-2_red"))
        let tabbarItem5 = UITabBarItem(title: "更多", image: UIImage(named: "more"), selectedImage: UIImage(named: "more_red"))
        
        //分别将五个UITabBarItem添加到各自的视图上
        rankController.tabBarItem = tabbarItem1
        searchController.tabBarItem = tabbarItem2
        pushController.tabBarItem = tabbarItem3
        circleController.tabBarItem = tabbarItem4
        moreController.tabBarItem = tabbarItem5
        
        //设置tabbar图标及字体被选中时的颜色,此处用五个页面的其中一个都可以
        rankController.tabBarController?.tabBar.tintColor = MAIN_RED

        //设置window的rootViewController为tabbarController,并让其显示
        self.window?.rootViewController = tabbarController
        self.window?.makeKeyAndVisible()
        return true
    }

这样我们就创建出了如下界面

基本界面.png

随后在各个界面中实现自己的代码即可

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现cl...
    以德扶人阅读 2,509评论 2 50
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,554评论 1 14
  • iOS 实战开发课程笔记 本贴旨在作为对极客班 《iOS 开发实战》第五期期课程视频重新学习的笔记。目标是建立一个...
    黄穆斌阅读 3,105评论 12 57
  • 偶尔逛qq空间的时候,看到一些很莫名其妙的说说。就感觉十分怪。 说什么:“最远的距离不是生与死,而是我站在你面前,...
    烟雨时节轻语笑阅读 169评论 0 0
  • 扩展NSMutableDictionary的一个类别,上面注释说的很清楚,除非value为nil的时候,调用方法 ...
    小笨憨阅读 277评论 0 0