Swift -标签页控制器(UITabBarController)用法

Swift -标签页控制器(UITabBarController)用法_第1张图片
屏幕快照 2016-11-08 19.12.46.png
Swift -标签页控制器(UITabBarController)用法_第2张图片
Simulator Screen Shot 2016年11月8日 19.11.36.png

首先创建三个Swift(HomeViewController.swift,CenterViewController.swift,MoreViewController.swift)用于TabBar的显示
1、接下来只需要在 AppDelegate.swift 文件里写code

import UIKit
 @UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
// 创建TabBar数组
var tabs = ["首页", "个人中心", "更多"]
var images = ["aa", "bb", "cc"]
var selectedImages = ["eee", "fff", "ggg"]

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    self.window = UIWindow.init(frame: UIScreen.main.bounds)
    self.window?.backgroundColor = UIColor.white
    
    let home = HomeViewController()
    let HomeNC = UINavigationController.init(rootViewController: home)
    
    // 改变图片 保证图片不失真
    let homeImage = UIImage(named:images[0])?.withRenderingMode(.alwaysOriginal)
    let homeSelectImage = UIImage(named:selectedImages[0])?.withRenderingMode(.alwaysOriginal)
    
    HomeNC.tabBarItem = UITabBarItem.init(title: "首页", image: homeImage, selectedImage: homeSelectImage)
    
    
    let center = CenterViewController()
    let CenterNC = UINavigationController.init(rootViewController: center)
    
    // 改变图片 保证图片不失真
    let centerImage = UIImage(named:images[1])?.withRenderingMode(.alwaysOriginal)
    let centerSelectImage = UIImage(named:selectedImages[1])?.withRenderingMode(.alwaysOriginal)
    
    CenterNC.tabBarItem = UITabBarItem.init(title: "个人中心", image: centerImage, selectedImage: centerSelectImage)
    // 下表数值显示
    CenterNC.tabBarItem.badgeValue = "10"
    
    
    
    let more = MoreViewController()
    let MoreNC = UINavigationController.init(rootViewController: more)
    
    // 改变图片 保证图片不失真
    let moreImage = UIImage(named:images[2])?.withRenderingMode(.alwaysOriginal)
    let moreSelectImage = UIImage(named:selectedImages[2])?.withRenderingMode(.alwaysOriginal)
    
    MoreNC.tabBarItem = UITabBarItem.init(title: "更多", image: moreImage, selectedImage: moreSelectImage)
    
    let navArray = [HomeNC, CenterNC, MoreNC]
    let tabBarController = UITabBarController()
    tabBarController.viewControllers = navArray
    self.window?.rootViewController = tabBarController
    
    
    self.window!.makeKeyAndVisible()
    
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}

首页文件---HomeViewController.swift ---

import UIKit
class HomeViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.title = "首页"
    self.view.backgroundColor = UIColor.lightGray
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

个人中心页面 ---CenterViewController.swift ---

import UIKit 
class CenterViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.title = "个人中心"
    self.view.backgroundColor = UIColor.red
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

更多的页面---MoreViewController.swift ---

import UIKit 
class MoreViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.title = "更多"
    self.view.backgroundColor = UIColor.green
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

下载Demo:https://github.com/silencesmile/Swift_UITabBarController

你可能感兴趣的:(Swift -标签页控制器(UITabBarController)用法)