UIKit框架中有各种Bar,UITabBar、UINavigationBar、UIToolbar。Bar对应的就有一些Item,tabBarItem、navigationItem、toolbarItems,再加上UIViewController、UINavigationController、UITabBarController很容易搞糊涂。我看了好久,没看明白。动手敲了下才有一点感觉。

一、联系

一个UINavigationController对应着一个UINavigationBar、UIToolbar,UIToolbar默认不显示,toolbarHidden=NO时显示。一个UITabBarController对应着一个UITabBar.一个UIViewController对应着一个tabBarItem、navigationItem和多个toolbarItems。navigationItem中可以设置左右按钮和中间视图等。

二、代码demo

1.首先代码结构 viewController1-viewController5用于生成5个tabBarItem。viewController、viewController6主要模拟登录注册,有时候需要先让用户登录注册之后才能进入。

2.代码

1.在AppDelegate.m中

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. tabBarViewController *tabVc=[[tabBarViewController alloc]init];
  3. [self.window makeKeyAndVisible];
  4. self.window.backgroundColor=[UIColor whiteColor];
  5. self.window.rootViewController=tabVc;
  6. return YES;
  7. }

2.ViewController.m

  1. //
  2. // ViewController.m
  3. // UITabBarController
  4. //
  5. // Created by City--Online on 15/5/26.
  6. // Copyright (c) 2015年 XQB. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "ViewController6.h"
  11.  
  12. @interface ViewController ()
  13.  
  14. @end
  15. @implementation ViewController
  16.  
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. NSLog(@"%@ %@",NSStringFromCGRect(self.tabBarController.tabBar.frame),NSStringFromCGRect(self.navigationController.toolbar.frame));
  20. UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
  21. btn.frame=CGRectMake(100, 100, 100, 100);
  22. [btn setTitle:@"按钮" forState:UIControlStateNormal];
  23. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  24. [self.view addSubview:btn];
  25. UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:nil];
  26. self.toolbarItems=@[item1];
  27. }
  28. -(void)btnClick:(id)sender
  29. {
  30. ViewController6 *vc6=[[ViewController6 alloc]init];
  31. vc6.title=@"第6页";
  32. vc6.hidesBottomBarWhenPushed=YES;
  33. [self.navigationController pushViewController:vc6 animated:YES];
  34. }
  35. - (void)didReceiveMemoryWarning {
  36. [super didReceiveMemoryWarning];
  37. // Dispose of any resources that can be recreated.
  38. }
  39. @end

3.ViewController1.m

  1. //
  2. // ViewController1.m
  3. // UITabBarController
  4. //
  5. // Created by City--Online on 15/5/26.
  6. // Copyright (c) 2015年 XQB. All rights reserved.
  7. //
  8.  
  9. #import "ViewController1.h"
  10. #import "ViewController2.h"
  11.  
  12. @interface ViewController1 ()
  13.  
  14. @end
  15.  
  16. @implementation ViewController1
  17.  
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20.  
  21. }
  22.  
  23. - (void)didReceiveMemoryWarning {
  24. [super didReceiveMemoryWarning];
  25. // Dispose of any resources that can be recreated.
  26. }
  27. @end

4.ViewController2.m

  1. //
  2. // ViewController2.m
  3. // UITabBarController
  4. //
  5. // Created by City--Online on 15/5/26.
  6. // Copyright (c) 2015年 XQB. All rights reserved.
  7. //
  8.  
  9. #import "ViewController2.h"
  10. #import "ViewController.h"
  11. #import "navigationViewController.h"
  12.  
  13. @interface ViewController2 ()
  14.  
  15. @end
  16.  
  17. @implementation ViewController2
  18.  
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. ViewController *vc=[[ViewController alloc]init];
  22. // navigationViewController *nav=[[navigationViewController alloc]initWithRootViewController:vc];
  23. // self.navigationController.toolbarHidden=NO;
  24. // nav.toolbarHidden=NO;
  25. [self.navigationController addChildViewController:vc];
  26.  
  27. }
  28.  
  29. - (void)didReceiveMemoryWarning {
  30. [super didReceiveMemoryWarning];
  31. // Dispose of any resources that can be recreated.
  32. }
  33.  
  34. @end

5.ViewController6.m

  1. //
  2. // ViewController6.m
  3. // UITabBarController
  4. //
  5. // Created by City--Online on 15/5/26.
  6. // Copyright (c) 2015年 XQB. All rights reserved.
  7. //
  8.  
  9. #import "ViewController6.h"
  10.  
  11. @interface ViewController6 ()
  12.  
  13. @end
  14.  
  15. @implementation ViewController6
  16.  
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. NSLog(@"%@ %@",NSStringFromCGRect(self.tabBarController.tabBar.frame),NSStringFromCGRect(self.navigationController.toolbar.frame));
  20. UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(btnClick:)];
  21. self.toolbarItems=@[item1];
  22. }
  23. -(void)btnClick:(id)sender
  24. {
  25. [self.navigationController popToRootViewControllerAnimated:YES];
  26. }
  27. - (void)didReceiveMemoryWarning {
  28. [super didReceiveMemoryWarning];
  29. // Dispose of any resources that can be recreated.
  30. }
  31.  
  32. @end

6.tabBarViewController.m继承UITabBarController

  1. //
  2. // tabBarViewController.m
  3. // UITabBarController
  4. //
  5. // Created by City--Online on 15/5/26.
  6. // Copyright (c) 2015年 XQB. All rights reserved.
  7. //
  8.  
  9. #import "tabBarViewController.h"
  10. #import "ViewController.h"
  11. #import "ViewController1.h"
  12. #import "ViewController2.h"
  13. #import "ViewController3.h"
  14. #import "ViewController4.h"
  15. #import "ViewController5.h"
  16. #import "navigationViewController.h"
  17.  
  18. @interface tabBarViewController ()<UITabBarControllerDelegate>
  19.  
  20. @end
  21.  
  22. @implementation tabBarViewController
  23.  
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. [self steup];
  27. self.delegate=self;
  28. self.selectedIndex=2;
  29. self.moreNavigationController.tabBarItem=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:1001];
  30. self.moreNavigationController.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:nil];
  31. self.moreNavigationController.visibleViewController.navigationItem.title=@"更多功能";
  32. self.moreNavigationController.visibleViewController.navigationItem.rightBarButtonItem.title=@"编辑";
  33.  
  34. }
  35. -(void)rightClick:(id)sender
  36. {
  37.  
  38. }
  39. -(void)steup
  40. {
  41. ViewController *vc=[[ViewController alloc]init];
  42. vc.title=@"第0页";
  43. navigationViewController *nvc=[[navigationViewController alloc]initWithRootViewController:vc];
  44. nvc.toolbarHidden=NO;
  45. UITabBarItem *tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第0页" image:[UIImage imageNamed:@"tabbar_homepage_normal"] selectedImage:[UIImage imageNamed:@"tabbar_homepage_selected"]];
  46. tabBarItem.badgeValue=@"2";
  47. nvc.tabBarItem=tabBarItem;
  48.  
  49. ViewController1 *vc1=[[ViewController1 alloc]init];
  50. vc1.title=@"第1页";
  51. navigationViewController *nvc1=[[navigationViewController alloc]initWithRootViewController:vc1];
  52. nvc1.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第1页" image:[UIImage imageNamed:@"tabbar_convenience_normal"] selectedImage:[UIImage imageNamed:@"tabbar_convenience_selected"]];
  53.  
  54. ViewController2 *vc2=[[ViewController2 alloc]init];
  55. vc2.title=@"第2页";
  56.  
  57. navigationViewController *nvc2=[[navigationViewController alloc]initWithRootViewController:vc2];
  58. nvc2.toolbarHidden=NO;
  59. nvc2.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第2页" image:[UIImage imageNamed:@"tabbar_electrice_commerce_normal"] selectedImage:[UIImage imageNamed:@"tabbar_electrice_commerce_selected"]];
  60.  
  61. ViewController3 *vc3=[[ViewController3 alloc]init];
  62. vc3.title=@"第3页";
  63. navigationViewController *nvc3=[[navigationViewController alloc]initWithRootViewController:vc3];
  64. vc3.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第3页" image:[UIImage imageNamed:@"tabbar_me_normal"] selectedImage:[UIImage imageNamed:@"tabbar_me_selected"]];
  65.  
  66. ViewController4 *vc4=[[ViewController4 alloc]init];
  67. vc4.title=@"第4页";
  68. navigationViewController *nvc4=[[navigationViewController alloc]initWithRootViewController:vc4];
  69. nvc4.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第4页" image:[UIImage imageNamed:@"tabbar_convenience_normal"] selectedImage:[UIImage imageNamed:@"tabbar_convenience_selected"]];
  70.  
  71. ViewController5 *vc5=[[ViewController5 alloc]init];
  72. vc5.title=@"第5页";
  73. navigationViewController *nvc5=[[navigationViewController alloc]initWithRootViewController:vc5];
  74. nvc5.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第5页" image:[UIImage imageNamed:@"tabbar_homepage_normal"] selectedImage:[UIImage imageNamed:@"tabbar_homepage_selected"]];
  75. // 默认的顺序
  76. NSArray *defaultarr=@[nvc,nvc1,nvc2,nvc3,nvc4,nvc5];
  77. // 自定义的顺序
  78. NSMutableArray *newarr=[[NSMutableArray alloc]init];
  79. //获取保存的title数组
  80. NSArray *titles=[[NSUserDefaults standardUserDefaults] arrayForKey:@"vcs"];
  81. // 第一次启动为nil
  82. if (titles==nil) {
  83. newarr=[defaultarr copy];
  84. }
  85. //根据自定义title数组 设置newarr数组
  86. for (NSString *s in titles) {
  87. for (navigationViewController *nvc in defaultarr) {
  88. if ([s isEqualToString:nvc.visibleViewController.title]) {
  89. [newarr addObject:nvc];
  90. }
  91. }
  92. }
  93. self.viewControllers=newarr;
  94.  
  95. }
  96. //UITabBarControllerDelegate
  97. //是否可以选中
  98. - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
  99. {
  100. return YES;
  101. }
  102. //选中某个viewController
  103. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
  104. {
  105. // NSLog(@"%@",viewController.title);
  106. }
  107. //开始自定义viewControllers
  108. - (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
  109. {
  110. NSLog(@"%@",viewControllers);
  111. }
  112. //即将编辑结束
  113. - (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
  114. {
  115. NSLog(@"%d",changed);
  116. }
  117. //编辑结束
  118. - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
  119. {
  120. //记下选择的选项顺序,方便下次启动时显示
  121. NSMutableArray *arr=[[NSMutableArray alloc]init];
  122. if (!changed) {
  123. return;
  124. }
  125. for (UIViewController *vc in viewControllers) {
  126. [arr addObject:vc.title];
  127. }
  128. NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
  129. [defaults setObject:arr forKey:@"vcs"];
  130.  
  131. }
  132.  
  133. - (void)didReceiveMemoryWarning {
  134. [super didReceiveMemoryWarning];
  135. // Dispose of any resources that can be recreated.
  136. }
  137.  
  138. /*
  139. #pragma mark - Navigation
  140.  
  141. // In a storyboard-based application, you will often want to do a little preparation before navigation
  142. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  143. // Get the new view controller using [segue destinationViewController].
  144. // Pass the selected object to the new view controller.
  145. }
  146. */
  147.  
  148. @end

7.navigationViewController.m

  1. //
  2. // navigationViewController.m
  3. // UITabBarController
  4. //
  5. // Created by City--Online on 15/5/26.
  6. // Copyright (c) 2015年 XQB. All rights reserved.
  7. //
  8.  
  9. #import "navigationViewController.h"
  10.  
  11. @interface navigationViewController ()<UINavigationControllerDelegate>
  12.  
  13. @end
  14.  
  15. @implementation navigationViewController
  16.  
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. // if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
  20. // {
  21. // self.edgesForExtendedLayout = UIRectEdgeNone;
  22. // }
  23. self.navigationController.navigationBar.translucent=NO;
  24. self.delegate=self;
  25.  
  26. }
  27. - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
  28. {
  29. NSLog(@"%@",viewController.title);
  30. }
  31. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
  32. {
  33. }
  34.  
  35. - (void)didReceiveMemoryWarning {
  36. [super didReceiveMemoryWarning];
  37. // Dispose of any resources that can be recreated.
  38. }
  39.  
  40. /*
  41. #pragma mark - Navigation
  42.  
  43. // In a storyboard-based application, you will often want to do a little preparation before navigation
  44. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  45. // Get the new view controller using [segue destinationViewController].
  46. // Pass the selected object to the new view controller.
  47. }
  48. */
  49.  
  50. @end

实现效果:

UIKit 框架之Bar、Controller的更多相关文章

  1. Tab Bar Controller和Navigation Controller混合使用详细教程

    在IPHONE上,NAV和TAB混合使用的案例很多.但很多书籍都没详细介绍这个是怎么使用的.我也找了很久才弄清楚怎么做.现在分享给大家. 1.先建立一个Window-based Application ...

  2. [New learn] UIKit 框架类

    NSObject NSObject is the root class of most Objective-C class hierarchies. NSDataAsset The NSDataAss ...

  3. UIKit框架使用总结--看看你掌握了多少

    一.经常使用的,基本就是每次项目迭代都需要使用的 UIView.UILabel.UIImage.UIColor.UIFont.UIImageView.UITextField.UIButton. UIS ...

  4. Swift - 重写UIKit框架类的init初始化方法(以UITabBarController为例)

    原来写了篇文章讲UITabBarController的用法,当时是从UIViewController跳转到UITabBarController页面,代码如下: 1 self.presentViewCo ...

  5. iOS第八课——Navigation Controller和Tab bar Controller

    今天我们要学习Navigation Controller和Tab bar Controller. Navigation Controller是iOS编程中比较常用的一种容器,用来管理多个视图控制器. ...

  6. UIKit框架

    在今后的应用程序构建中,会陆续使用各式各样的控件,因此UIKit框架的引入是必不可少的! 一.简介 UIKitk框架提供一系列的Class(类)来建立和管理iPhone OS应用程序的用户界面接口.应 ...

  7. iOS开发中的错误整理,Changing the delegate of a tab bar managed by a tab bar controller is not allowed

    iOS [错误:'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'] 错误:'Ch ...

  8. iOS学习32之UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

  9. 基础框架Fundation和UIkit框架的定义和使用

    Foundation 框架为所有应用程序提供基本的系统服务 您的应用程序以及 UIKit 和其他框架,都建立在 Foundation 框架的基础结构之上.Foundation 框架提供许多基本的对象类 ...

随机推荐

  1. JavaScript中使用function作为对象键值

    JavaScript的键值只能是string或者number,这一点真是返祖现象啊.现在我面临的问题: var funcs = {}; var funcA = function() { }; var ...

  2. lnmp下thinkphp 500错误指南

    先在php.ini打开报错,display_errors: on: 如果是open_basedir的问题,修改nginx的配置文件fastcgi.conf 将fastcgi_param PHP_ADM ...

  3. 网易郑栋:数据采集与分析的那些事——从数据埋点到AB测试

    本文由  网易云发布. 4月8日晚,DTalk邀请到了网易互联网分析产品.可视化 BI 产品的负责人—郑栋老师,进行了一次关于<网易郑栋:数据采集与分析的那些事第一弹: 数据篇>的主题分享 ...

  4. kernel 调试 打印IP地址

    #define NIPQUAD(addr) \ ((unsigned char *)&addr)[0], \ ((unsigned char *)&addr)[1], \ ((unsi ...

  5. 《Python绝技:运用Python成为顶级黑客》 用Python进行取证调查

    1.你曾经去过哪里?——在注册表中分析无线访问热点: 以管理员权限开启cmd,输入如下命令来列出每个网络显示出profile Guid对网络的描述.网络名和网关的MAC地址: reg query &q ...

  6. Django路由配置系统、视图函数

    一.路由配置系统(URLconf) URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于这个 ...

  7. 使用PhpSpreadsheet将Excel导入到MySQL数据库

    本文以导入学生成绩表为例,给大家讲解使用PhpSpreadsheet将Excel导入的MySQL数据库. 准备 首先我们需要准备一张MySQL表,表名t_student,表结构如下: CREATE T ...

  8. 二,windows下安装memcached服务

    window下安装memcached服务的流程如下: 1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached 2. 在终端(也即cmd命令界面)下输入 ‘c ...

  9. 记录php漏洞--宇宙最强语言 PHP 爆出 DoS 漏洞,可以直接灌满 CPU

    站长之家(Chinaz.com)5月20日消息  近日,PHP被爆出存在远程DOS漏洞,若黑客利用该漏洞构造PoC发起连接,容易导致目标主机CPU被迅速消耗.此漏洞涉及众多PHP版本,因而影响范围极大 ...

  10. C的Define

    #define Conn(x,y) x##y  //表示x连接y #define ToChar(x) #@x //给x加上单引号 #define ToString(x) #x  //给x加双引号 #d ...