UITabBarController的使用

//
//  AppDelegate.swift
//  TabbarWithCode
//
//  Created by admin on 16/1/22.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        let window = UIWindow(frame:UIScreen.mainScreen().bounds)
        let tabbar = UITabBarController() // 初始化tabbar控制器
        window.rootViewController = tabbar // 设置window根控制器
        window.makeKeyAndVisible()
        self.window = window

        let c1 = UIViewController()
        c1.view.backgroundColor = UIColor.grayColor()
        c1.tabBarItem.title = "通讯录" // tabbarbutton的标题
        c1.tabBarItem.image = UIImage(imageLiteral: "tab_buddy_nor") // tabbarbutton的图片
        c1.tabBarItem.badgeValue = "10" // tabbarbutton显示的数字
//        tabbar.addChildViewController(c1) // 逐条添加
        let c2 = UIViewController()
        c2.view.backgroundColor = UIColor.blueColor()
        c2.tabBarItem.title = "设置"
        c2.tabBarItem.image = UIImage(imageLiteral: "tab_me_nor")
        c2.tabBarItem.badgeValue = "2"
//        tabbar.addChildViewController(c2)
        let c3 = UIViewController()
        c3.view.backgroundColor = UIColor.blackColor()
        c3.tabBarItem.title = "动态"
        c3.tabBarItem.image = UIImage(imageLiteral: "tab_qworld_nor")
        c3.tabBarItem.badgeValue = "8"
//        tabbar.addChildViewController(c3)
        let c4 = UIViewController()
        c4.view.backgroundColor = UIColor.whiteColor()
        c4.tabBarItem.title = "消息"
        c4.tabBarItem.image = UIImage(imageLiteral: "tab_recent_nor")
        c4.tabBarItem.badgeValue = "99"
//        tabbar.addChildViewController(c4)
        tabbar.viewControllers = [c1,c2,c3,c4]// 一次性添加
        return true
    }

    func applicationWillResignActive(application: UIApplication) {

    }

    func applicationDidEnterBackground(application: UIApplication) {
            }

    func applicationWillEnterForeground(application: UIApplication) {

    }

    func applicationDidBecomeActive(application: UIApplication) {

    }

    func applicationWillTerminate(application: UIApplication) {

    }
}

标签: swift, ios控件

添加新评论