目錄:[Swift]Xcode實際操做html
本文將爲你演示,選項卡視圖控制器的建立和使用。swift
在項目文件夾【DemoApp】上點擊鼠標右鍵,彈出右鍵菜單。數組
【New File】->【Cocoa Touch Class】->【Next】->app
【Class】:FirstSubViewControlleride
【Subclass of】:UIViewControllerpost
【Language】:Swiftui
->【Next】->【Create】this
1 import UIKit 2 3 class FirstSubViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 // Do any additional setup after loading the view. 9 //設置當前視圖控制器,在選項卡視圖控制器中的標題 10 self.title = "Item #1" 11 //接着設置當前視圖控制器的選項卡圖標 12 self.tabBarItem.image = UIImage(named: "home") 13 //設置當前視圖控制器的選項卡被選中時的圖標 14 self.tabBarItem.selectedImage = UIImage(named: "home") 15 //再設置當前視圖控制器的背景顏色爲棕色 16 self.view.backgroundColor = UIColor.brown 17 } 18 19 override func didReceiveMemoryWarning() { 20 super.didReceiveMemoryWarning() 21 // Dispose of any resources that can be recreated. 22 } 23 }
建立第二個視圖控制器。spa
在項目文件夾【DemoApp】上點擊鼠標右鍵,彈出右鍵菜單。代理
【New File】->【Cocoa Touch Class】->【Next】->
【Class】:SecondSubViewController
【Subclass of】:UIViewController
【Language】:Swift
->【Next】->【Create】
1 import UIKit 2 3 class SecondSubViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 // Do any additional setup after loading the view. 9 //設置當前視圖控制器,在選項卡視圖控制器中的標題 10 self.title = "Item #2" 11 //接着設置當前視圖控制器的選項卡圖標 12 self.tabBarItem.image = UIImage(named: "settings") 13 14 //再設置當前視圖控制器的背景顏色爲紫色 15 self.view.backgroundColor = UIColor.purple 16 } 17 18 //重寫控制器的視圖即將顯示時的方法, 19 //當控制器即將顯示時,執行該方法。 20 override func viewWillAppear(_ animated: Bool) { 21 super.viewWillAppear(animated) 22 //給選項卡的圖標,添加一個數字9的角標 23 self.tabBarItem.badgeValue = "9" 24 } 25 26 override func didReceiveMemoryWarning() { 27 super.didReceiveMemoryWarning() 28 // Dispose of any resources that can be recreated. 29 } 30 }
從【Assets.xcassets】資源文件夾中導入兩張圖片。
【+】->【Import】->選擇圖片->【Open】
打開【AppDelegate.swift】應用程序的代理文件
1 import UIKit 2 3 @UIApplicationMain 4 class AppDelegate: UIResponder, UIApplicationDelegate { 5 6 var window: UIWindow? 7 //在應用啓動完成的代理方法中,建立程序的入口 8 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 9 // Override point for customization after application launch. 10 //實例化第一個視圖控制器對象 11 let viewController1 = FirstSubViewController() 12 //接着實例化第二個視圖控制器對象 13 let viewController2 = SecondSubViewController() 14 15 //再初始化一個選項卡控制器對象 16 let tabViewController = UITabBarController() 17 //將兩個視圖控制器對象,以數組的方法,指定給選項卡控制器對象 18 tabViewController.viewControllers = [viewController1, viewController2] 19 //設置選項卡控制器顯示第一個子控制器所對應的頁面 20 tabViewController.selectedIndex = 0 21 //設置選項卡控制器對象,根視圖背景顏色爲白色 22 tabViewController.view.backgroundColor = UIColor.white 23 //將選項卡控制器對象,做爲當前窗口的根視圖控制器 24 self.window?.rootViewController = tabViewController 25 26 //還能夠在此設置各個選項卡的圖標,首先得到選項卡控制器的標籤條 27 let tabBar = tabViewController.tabBar 28 //而後得到標籤條中的第一個選項卡標籤 29 let item = tabBar.items![0] 30 //設置第一個標籤的圖標 31 item.image = UIImage(named: "home") 32 //設置第一個標籤的標題文字 33 item.title = "home" 34 35 //接着得到標籤條中的第二個選項卡標籤 36 let item2 = tabBar.items![1] 37 //設置第二個標籤的圖標 38 item2.image = UIImage(named: "settings") 39 //設置第二個標籤的標題文字 40 item2.title = "settings" 41 //設置位於右上角的角標所顯示的數字 42 item2.badgeValue = "8" 43 44 return true 45 } 46 47 func applicationWillResignActive(_ application: UIApplication) { 48 // 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. 49 // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 50 } 51 52 func applicationDidEnterBackground(_ application: UIApplication) { 53 // 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. 54 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 55 } 56 57 func applicationWillEnterForeground(_ application: UIApplication) { 58 // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 59 } 60 61 func applicationDidBecomeActive(_ application: UIApplication) { 62 // 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. 63 } 64 65 func applicationWillTerminate(_ application: UIApplication) { 66 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 67 } 68 }