本文将为你演示,选项卡视图控制器的创建和使用。
在项目文件夹【DemoApp】上点击鼠标右键,弹出右键菜单。
【New File】->【Cocoa Touch Class】->【Next】->
【Class】:FirstSubViewController
【Subclass of】:UIViewController
【Language】:Swift
->【Next】->【Create】
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 }
创建第二个视图控制器。
在项目文件夹【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 }
原文地址:https://www.cnblogs.com/strengthen/p/10389213.html
时间: 2024-10-11 17:21:34