iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >iOS开发中UITabBarController的使用示例
  • 189
分享到

iOS开发中UITabBarController的使用示例

iOS 2022-05-22 00:05:51 189人浏览 泡泡鱼
摘要

首先我们看一下它的view层级图: - (BOOL)application:(UIApplication *)application didFinishLaunchingWith

首先我们看一下它的view层级图:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  {      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];      // Override point for customization after application launch.      self.window.backgroundColor = [UIColor whiteColor];     #pragma mark - 设置tabBarItem  #pragma mark  第一个视图ViewController            HMT_AViewController * tabBarViewA = [[HMT_AViewController alloc] init];      // 设置A视图下----标签栏标题文字(可参照微信或者QQ体会)      tabBarViewA.tabBarItem.title = @"微信";      // 设置A视图下----标签栏图片(因为自己没有图片,在这里随便设置了个名字)      //tabBarViewA.tabBarItem.image = [UIImage imageNamed:@"1.png"];      // 设置A视图下----标签栏信息提示(住:badgeValue是NSString类型 如下设置了3,就像QQ消息有3条未接受一样,给人一种提醒)      tabBarViewA.tabBarItem.badgeValue = @"3";      // iOS7弃用了----标签栏选中的时候显示一张图片,没选中的时候显示另一张图片      //[tabBarViewA.tabBarItem setFinishedSelectedImage:actionMenu.selectedIcon withFinishedUnselectedImage:actionMenu.icon];      // ios7的方法(自己没有图片,所以代码里面的图片都是一个随便取的名字,没有实用意义)      //tabBarViewA.tabBarItem.selectedImage = actionMenu.selectedIcon;        #pragma mark  第二个视图ViewController      // 第二个视图ViewController      HMT_BViewController * tabBarViewB = [[HMT_BViewController alloc] init];      // 设置B视图下----标签栏      // 用系统提供的标识(可以算等价于图标和文字)进行设置(参数:UITabBarSystemItem是个枚举值,想要什么形式,就去系统提供的api中找)      tabBarViewB.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];      // 设置B视图下----标签栏信息提示      tabBarViewB.tabBarItem.badgeValue = @"Go";        #pragma mark  第三个视图ViewController      HMT_CViewController * tabBarViewC = [[HMT_CViewController alloc] init];      tabBarViewC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:2];      // 设置B视图下----标签栏信息提示      tabBarViewC.tabBarItem.badgeValue = @"new";        #pragma mark  第四个视图ViewController      HMT_DViewController * tabBarViewD = [[HMT_DViewController alloc] init];      tabBarViewD.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];      // 设置B视图下----标签栏信息提示      tabBarViewD.tabBarItem.badgeValue = @"99";        #pragma mark  第五个视图ViewController      HMT_EViewController * tabBarViewE = [[HMT_EViewController alloc] init];      tabBarViewE.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:4];      // 设置B视图下----标签栏信息提示      tabBarViewE.tabBarItem.badgeValue = @"sky";        #pragma mark  第六个视图ViewController(系统默认能显示的最大视图个数是5个)            HMT_FViewController * tabBarViewF = [[HMT_FViewController alloc] init];      tabBarViewF.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:5];      // 设置F视图下----标签栏信息提示      tabBarViewF.tabBarItem.badgeValue = @"AG";                    #pragma mark - 设置TabBarController            // 创建TabBarController      UITabBarController * tabBarController = [[UITabBarController alloc]init];      // TabBarController默认是放在最底部的,如果你想调整位置,可以进行下面2部操作(44是iPhone中TabBarController和UINavigationController标准高度)      //CGRect frame = CGRectMake(0, 20, 320, 44);      //tabBarController.tabBar.frame = frame;      // 每一个tab都必须有一个content view controller------->viewControllers属性,用来存入一个应用的TabBarController有多少个界面切换      tabBarController.viewControllers = [NSArray arrayWithObjects:tabBarViewA,tabBarViewB,tabBarViewC,tabBarViewD,tabBarViewE,tabBarViewF, nil nil];      // 设置着色      tabBarController.tabBar.tintColor = [UIColor greenColor];      // 设置选中图片时候      tabBarController.tabBar.selectedImageTintColor = [UIColor brownColor];      // 设置背景图片(自己没有图片,不进行设置)      //tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"@@@@@"];      // 设置程序启动时默认的ViewController视图(设置为3,一共5个ViewController,进来时候显示的视图就是第4个-tabBarViewD,下标从0开始)      tabBarController.selectedIndex = 3;                  self.window.rootViewController = tabBarController;            [self.window makeKeyAndVisible];      return YES;  } 

最后效果如下图:

UITabBarController的代理方法以及模态显示 首先要实现协议<UITabBarControllerDelegate>

    // 设置代理     tabBarController.delegate =self;      //UINavigationController *nav = tabBarController.moreNavigationController;     //[nav setNavigationBarHidden:YES animated:YES];

// 控制哪些ViewController的标签栏能被点击 - (BOOL)tabBarController:(UITabBarController *)tabBarControllershouldSelectViewController:(UIViewController *)viewController{     // 代表HMT_CViewController这个View无法显示,无法点击到它代表的标签栏     if ([viewControllerisKindOfClass:[HMT_CViewControllerclass]]) {         returnNO;     }     returnYES; }

// 选中哪个标签栏,一个监控作用吧 - (void)tabBarController:(UITabBarController *)tabBarControllerdidSelectViewController:(UIViewController *)viewController{

}

// More view controller将要开始编辑 - (void)tabBarController:(UITabBarController *)tabBarControllerwillBeginCustomizingViewControllers:(NSArray *)viewControllers{

} // More view controller将要结束编辑 - (void)tabBarController:(UITabBarController *)tabBarControllerwillEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{

} // More view controller编辑 - (void)tabBarController:(UITabBarController *)tabBarControllerdidEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{

}

#import "HMT-AViewController.h" #import "HMTModalShowViewController.h"

@interfaceHMT_AViewController () @end

@implementation HMT_AViewController

- (void)viewDidLoad {     [superviewDidLoad];     self.view.backgroundColor = [UIColorredColor];         // 创建一个按钮     UIButton * button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];     button.frame =CGRectMake(100,100,100, 100);     [button addTarget:self action:@selector(modalShow)forControlEvents:UIControlEventTouchUpInside];     [self.view addSubview:button]; // Do any additional setup after loading the view. }

- (void)modalShow{         HMTModalShowViewController * modalShowVC = [[HMTModalShowViewController alloc]init];

    //模态视图控制器呈现出来时候的视觉效果     modalShowVC.modalTransitionStyle =UIModalTransitionStyleCrossDissolve;         //模态视图控制器呈现方式,默认全屏     modalShowVC.modalPresentationStyle =UIModalPresentationFullScreen;             UINavigationController * modalShowNC = [[UINavigationController alloc] initWithRootViewController:modalShowVC];         //推出模态视图控制器     [self presentViewController:modalShowNC animated:YES completion:^{         NSLog(@"hello world");           }]; }

#import "HMTModalShowViewController.h"

@interfaceHMTModalShowViewController ()

@end

@implementation HMTModalShowViewController

- (void)viewDidLoad {     [superviewDidLoad]; // Do any additional setup after loading the view.         self.view.backgroundColor = [UIColor yellowColor];         // 利用UINavigationController来实现退出控制器     UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(modalDismiss)];         self.navigationItem.leftBarButtonItem = barButton;     self.navigationItem.title =@"humingtao";         //创建一个按钮来实现退出控制器     }

- (void)modalDismiss{    //退出模态视图控制器     [self dismissViewControllerAnimated:YES completion:^{         NSLog(@"退出GoodBye");     }]; } @end

--结束END--

本文标题: iOS开发中UITabBarController的使用示例

本文链接: https://www.lsjlt.com/news/30119.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • iOS开发之UIMenuController使用示例详解
    目录简介接口介绍使用探索如何创建并显示 UIMenuController实现 Item 点击事件菜单 Item 太多???UIResponderStandardEditActions...
    99+
    2024-04-02
  • iOS中uitabbarcontroller的作用是什么
    UITabBarController是一个容器视图控制器,用于管理多个子视图控制器,并通过标签栏(Tab Bar)的方式让用户可以轻...
    99+
    2024-04-03
    iOS
  • iOS中uitabbarcontroller的用法是什么
    UITabBarController是一个容器控制器,用于管理多个子视图控制器,用户可以通过标签栏进行快速切换。每个子视图控制器对应...
    99+
    2024-04-03
    iOS
  • IOS开发Objective-C Runtime使用示例详解
    目录前言一些关键字消息传递 (Messaging)KVO关联对象 (Associated Objects)AOP(Method Swizzling)其它前言 Runtime&nbs...
    99+
    2023-02-13
    Objective-C Runtime iOS开发
  • iOS开发学习ViewController使用示例详解
    目录iOS ViewControllerWhat is a View Controller如何定义ViewController.ViewController的类型生命周期回调View...
    99+
    2022-11-13
    iOS开发ViewControlle iOS ViewControlle
  • iOS开发frame和bounds使用示例详解
    目录简述frame和bounds何时使用Frame,何时使用Bounds简述 Frame: 视图的位置和大小使用是父视图的坐标系,所以将视图放置在父级中这一点就很重要。 Bounds...
    99+
    2024-04-02
  • Android/iOS内嵌Unity开发示例
    目录 前言 背景 正文 环境 新建工程 Unity导出 Android接入 如何使用 作为Activity 总结 1.Android调用Unity 2.Unity调用Android 3.C/C++“中转站” 参考资料 前言 背景 Un...
    99+
    2023-09-06
    android unity 游戏引擎
  • iOS 11开发中iOS11应用视图的示例分析
    这篇文章给大家分享的是有关iOS 11开发中iOS11应用视图的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。在iPhone或者iPad中,用户看到的和摸到的都是视图。视图是用户界面的重要组成元素。本节将...
    99+
    2023-06-04
  • iOS 11开发中iOS11模拟器的示例分析
    这篇文章将为大家详细讲解有关iOS 11开发中iOS11模拟器的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。iOS11模拟器介绍在图1.6或者1.7中所看到的类似于手机的模型就是iOS模拟器。i...
    99+
    2023-06-04
  • iOS中NSThread使用示例详解
    目录正文创建和启动线程线程的状态线程安全原子和非原子属性@synchronized线程间通信正文 NSThread的对象就代表一条线程,轻量级的线程操作,生命周期需要程序员控制,当任...
    99+
    2022-11-13
    iOS NSThread 使用 iOS NSThread
  • iOS开发中AvaudioPlayer怎么使用
    在iOS开发中,你可以使用AVAudioPlayer类来播放音频文件。下面是使用AVAudioPlayer的基本步骤:1. 导入AV...
    99+
    2023-09-13
    iOS
  • iOS开发中nstimeinterval怎么使用
    NSTimeInterval是一个双精度浮点型的时间间隔,它表示自2001年1月1日午夜(GMT)以来的秒数。在iOS开发中,我们可...
    99+
    2023-08-24
    nstimeinterval
  • iOS开发中rangeOfString怎么使用
    rangeOfString是一个NSString类的方法,用于在字符串中查找指定的子字符串。它返回一个NSRange结构体,指示子字...
    99+
    2023-09-15
    iOS
  • ios开发中如何使用navigationBar隐藏显示的过度
    这篇文章给大家分享的是有关ios开发中如何使用navigationBar隐藏显示的过度的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。navigationBar隐藏显示的过度相信在...
    99+
    2024-04-02
  • IOS开发自定义view方法规范示例
    目录前言一、关于自定义View的初始化方法二、关于addSubview三、关于layoutSubviews四、关于frame与bounds总结前言 对于接触业务开发的童鞋,自定义Vi...
    99+
    2024-04-02
  • iOS 11开发中iOS11应用视图位置和大小的示例分析
    这篇文章主要介绍iOS 11开发中iOS11应用视图位置和大小的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!当一个视图使用拖动的方式添加到主视图后,它的位置和大小可以使用拖动的方式进行设置,也可以使用尺寸检...
    99+
    2023-06-04
  • iOS 小组件开发 && iOS 小组件开发用到的技术
    iOS 小组件开发 iOS小组件开发是指在iOS设备的主屏幕上添加自定义的小组件,用于显示特定的信息或提供简化的交互。iOS 14及更高版本引入了小组件功能,使用户能够在主屏幕上自定义并快速访问相关内容。 以下是iOS小组件开发的基本步骤:...
    99+
    2023-10-05
    ios xcode swift 小组件开发
  • iOS开发探索多线程GCD队列示例详解
    目录引言进程与线程1.进程的定义2.线程的定义3、 进程和线程的关系4、 多线程5、 时间片6、 线程池GCD1、任务2、队列3、死锁总结引言 在iOS开发过程中,绕不开网络请求、下...
    99+
    2024-04-02
  • iOS开发底层探索界面优化示例详解
    目录1、卡顿原理1.1、界面显示原理1.2、界面撕裂1.3、界面卡顿小结2、卡顿检测2.1、CADisplayLink2.2、RunLoop检测2.3、微信matrix2.4、滴滴D...
    99+
    2024-04-02
  • iOS开发探索多线程GCD任务示例详解
    目录引言同步任务死锁异步任务总结引言 在上一篇文章中,我们探寻了队列是怎么创建的,串行队列和并发队列之间的区别,接下来我们在探寻一下GCD的另一个核心 - 任务 同步任务 void ...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作