[Xcode 实际操作]三、视图控制器-(2)UITabBarController选项卡(标签)视图控制器

本文将为你演示,选项卡视图控制器的创建和使用。

在项目文件夹【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-08-07 14:52:56

[Xcode 实际操作]三、视图控制器-(2)UITabBarController选项卡(标签)视图控制器的相关文章

[Xcode10 实际操作]三、视图控制器-(10)在Storyboard中使用图像视图控件

本文将演示常用的图像视图控件在故事板中的使用. 打开故事板文件[Main.storyboard]点击选择视图控制器的根视图. 点击库图标,打开控件库面板. 在控件库搜索框内,输入控件名称,在控件库中,快速定位目标控件. 然后在标签控件上双击,导入所需的控件. 在图像视图右侧的定界框上按下手指,并向右拖动,以调整标签视图的宽度. 将标签控件向下方拖动一段距离. 然后点击库图标,再次打开控件库面板. 在控件库搜索框内,输入控件名称,在控件库中,快速定位目标控件. 然后在按钮控件上双击,导入所需的控件

iOS基础之UITabBarController(标签视图控制器)

UITabBarController是可以帮我们添加.管理许多的标签项,使我们的程序包含不同的操作模式,由于管理UITabBar可能比较麻烦,系统帮我们对其进行了封装,产生了简单好用的UITabBarController--标签视图控制器. 代码演示: #import "AppDelegate.h" #import "FirstViewController.h" #import "SecondViewController.h" #import &

UITabBarController — 标签视图控制器

UITabBarController - 标签视图控制器 UITabBarController 分为三层结构: (1).tab bar (2.)Custom Content (3.). Tab bar controller View UITabBarController 有以下重要属性: (1).viewControls 显示的视图控制器 (2).tabBar 标签栏 (3).delegate 代理 (4).selectedindex 选中某个tabBarItme UITabBar (1).ta

iOS中的UITabBarController(标签视图控制器)

#import "AppDelegate.h" #import "FirstTableViewController.h" #import "SecondTableViewController.h" #import "ThirdTableViewController.h" #import "FourthTableViewController.h" #import "FiveTableViewCont

标签视图控制器UITabBarController

标签视图控制器 UITabBarController FirstViewController*first = [[FirstViewController alloc] init]; //创建一个UITableBarItem对象,作为first的 tabBarItem属性 //选中与不选中图片可以一样,可以不一样 UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"第一" image:[UIImage imageNamed

UITabBarController ---- 标签视图控制器

直接上代码: // // AppDelegate.m // // #import "AppDelegate.h" #import "RootViewController.h" #import "FirstViewController.h" #import "SecnodViewController.h" #import "ThirdViewController.h" @interface AppDelega

多控制器管理 UITabBarController

多控制器管理 UITabBarController,跟UINavigationController类似,UITabBarController也可以轻松管理多个控制器,轻松完成控制器之间的切换,例如QQ,微信. 头文件定义: @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding> @property(nonatomic,copy) NSArray *viewControllers; // If t

GIS基础软件及操作(三)

原文 GIS基础软件及操作(三) 练习三.地图配准操作 1.对无坐标信息的地形图(图片格式)进行地图配准操作2.编辑器的使用(点要素.线要素.多边形要素的数字化) 本例主要介绍如何给无坐标信息的地形图(图片格式)添加坐标信息.带有坐标信息的图片文件格式有tiff和grid 格式(此种情况可跳过1.2.3步,从第4步开始),其余如jpg.bmp.png等都不带有坐标信息. 第1步 确定目标坐标信息 打开地形图图片,查看该地形图的坐标基准信息.比例尺以及坐标范围.[坐标基准信息]为该地形图的地理坐标

iOS视图控制器编程指南 --- 呈现一个视图控制器

有两种方法实现一个视图控制器到屏幕上:把它嵌入到一个容器视图控制器或者是直接呈现它.容器视图控制器提供一个应用程序主要的导航功能,但是present 视图控制器也是一个重要的导航工具.你可以直接使用presentation 在当前视图控制器的最上层显示一个新的视图控制器.典型地,当你想要实现模态界面的时候直接present 视图控制器,但是你也可以基于其它目的使用它们. 对呈现视图控制器的支持内建于类UIViewController中,而且对所有的视图控制器对象都是有效的.你可以在其它任何视图控