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
    }

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

iOS开发基础--代码实现包含NavigationController和TabBarController的界面_第1张图片
基本界面.png

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

你可能感兴趣的:(iOS开发基础--代码实现包含NavigationController和TabBarController的界面)