下面记一下怎样通过代码的方式为选项卡添加视图。

1、创建一个基于Empty Application的项目

2、创建两个新类,基类选择UIViewController,勾选With XIB for user interface分别命名为"OneController'和"TwoController",

3、分别更改OneController.xib和TwoController.xib文件的view背景颜色,便于区分

4、在AppDelegate.m文件中的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ }函数做如下修改(记得导入OneController和TwoController的头文件)

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  4. //将tabBar(选项卡)添加进来
  5. UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
  6. //为选项卡添加子控制器
  7. OneController *one = [[[OneController alloc] init] autorelease];
  8. [tabBarController addChildViewController:one];
  9. TwoController *two = [[[TwoController alloc] init] autorelease];
  10. [tabBarController addChildViewController:two];
  11. self.window.rootViewController = tabBarController;
  12. [self.window makeKeyAndVisible];
  13. return YES;
  14. }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; //将tabBar(选项卡)添加进来
UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease]; //为选项卡添加子控制器
OneController *one = [[[OneController alloc] init] autorelease];
[tabBarController addChildViewController:one]; TwoController *two = [[[TwoController alloc] init] autorelease];
[tabBarController addChildViewController:two]; self.window.rootViewController = tabBarController; [self.window makeKeyAndVisible];
return YES;
}

运行效果如下:

现在创建好的选项卡下面是没有图标和文字的,,,现在我们通过代码给它们添加一些图标和文字,注意,,选项卡的图标和文字是子控制器决定的而不是tab Bar Controller,,这点要记住。

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  4. //将tabBar(选项卡)添加进来
  5. UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
  6. //为选项卡添加子控制器
  7. OneController *one = [[[OneController alloc] init] autorelease];
  8. one.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:0] autorelease];//增加系统自带的下载图标
  9. [tabBarController addChildViewController:one];
  10. TwoController *two = [[[TwoController alloc] init] autorelease];
  11. //添加一个自定义的图标和文字
  12. two.tabBarItem.title = @"two";
  13. two.tabBarItem.image = [UIImage imageNamed:@"success.png"];
  14. [tabBarController addChildViewController:two];
  15. self.window.rootViewController = tabBarController;
  16. [self.window makeKeyAndVisible];
  17. return YES;
  18. }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; //将tabBar(选项卡)添加进来
UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease]; //为选项卡添加子控制器
OneController *one = [[[OneController alloc] init] autorelease];
one.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:0] autorelease];//增加系统自带的下载图标
[tabBarController addChildViewController:one]; TwoController *two = [[[TwoController alloc] init] autorelease];
//添加一个自定义的图标和文字
two.tabBarItem.title = @"two";
two.tabBarItem.image = [UIImage imageNamed:@"success.png"];
[tabBarController addChildViewController:two]; self.window.rootViewController = tabBarController; [self.window makeKeyAndVisible];
return YES;
}

以上代码中,我在第一个Controll View 中添加了一个系统自带的下载图标,,在第二个Controller View中添加了一个自定义的图标(先将图标导入到项目中)和文字。

运行效果如下:

以上的所有代码我都是在AppDelegate.m文件中得

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ }函数中实现的,,,这仅仅只是为了操作方便才这样写的,,大多数情况下是写在该tab Bar Controller 的实现文件中的,如这里是在的MyTabController.m文件中的- (id)init{ } 函数中实现的。

tab bar controller的更多相关文章

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

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

  2. 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 ...

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

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

  4. Iphone [Tab Bar实现多view切换,Picker,DataPicter实现

    用Tab Bar Controller处理IPhone多个view切换, 而且还附有创建空项目,picker和DataPicker的实现! 具体步骤: 1.创建一个空项目,选择User Interfa ...

  5. iOS开发:使用Tab Bar切换视图

    iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...

  6. Swift 4.0.2 按下tab bar item时, item会有内缩的animation效果(如同Twitter的tab bar 效果一样)

    先上效果图: 假设 tab bar items 有5个.tag为0,1,2,3,4.storyboard中tab bar controller继承的class叫做xxxVC. class xxxVC: ...

  7. 学习笔记:Tab Bar 控件使用详解

    注意这里是:Tab Bar 不是Tab Bar Controller. Tab bar是继承UIView,所以可以添加到ViewController里.是View就可以add到另一个View上去.Ta ...

  8. IOS学习之基于IOS7的tab bar

    转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/28129473 作者:小马 什么是tabbar? 先几张图:      上图中蓝色框 ...

  9. uitabbarcontroller中 在设置tab bar item的image属性后不显示问题

    开始使用ios中的UITabBarController,在给Tab Bar Item设置自定义图片的时候,遇到了问题 按照如下配置: 出来的结果确是: 实际上test24.png应该是: 纠结了很久, ...

随机推荐

  1. liteos内存(三)

    1. 概述 1.1 基本概念 内存管理模块管理系统的内存资源,它是操作系统的核心模块之一.主要包括内存的初始化.分配以及释放. 在系统运行过程中,内存管理模块通过对内存的申请/释放操作,来管理用户和O ...

  2. hdoj4180

    题意: 使(a/b-c/d)最小,然后让你求c/d. 我们能说最小the error |A/B - C/D| 然后C,D的范围是 0 < C < D < B. 其实就是:求接近(A/ ...

  3. python 子类调用父类成员的方法

    1.直接写类名调用: parent_class.parent_attribute(self) class Animal(): def __init__(self, name): self.name = ...

  4. JAVA多线程(二) 并发队列和阻塞队列

    github代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service/ ...

  5. 如果没有intelliJ 编译器?

    刚刚是了一晚上的用记事本来写代码,脱离编译器,发现自己完全废了. 写了好多个类,在命令行编译失败,上网查错误,说编码问题, 编码问题解决后,命令行还是编译失败,粘贴到编译器 发现完全是自己代码能力太弱 ...

  6. Unity插值函数Lerp()与增量时间Time.deltatime

    一.Unity插值函数Lerp() 通过官方文档简单了解插值函数(https://docs.unity3d.com/ScriptReference/index.html),可以看到插值函数有很多 Ma ...

  7. Java反编译工具-JD-GUI

    Java是跨平台的,JD-GUI提供了多个系统的支持,但是不建议直接安装,最快的方式推荐直接下载JAR包,然后用java -jar进行运行. 就现在的版本是1.4.0,停留在2015年,估计近期会更新 ...

  8. python实现计数排序

    计数排序有局限性,最小值和最大值决定着数组的长度,如果分配均匀的话可以用 # @File: count_sort import random def count_sort(li, max_num=10 ...

  9. HDU6447(离散化扫描线+树状数组)

    一眼看过去就x排序扫描一下,y是1e9的离散化一下,每层用树状数组维护一下,然后像dp倒着循环似的树状数组就用y倒着插就可行了. 类似题目练习:BZOJ4653.BZOJ1218 #pragma co ...

  10. Win10 Hyper-v 中安装 CentOS 搭建开发环境

    Windows 环境 操作系统:Windows 10 开发环境:VS2005(需启动.NET Framework 3.5 ,才能正常安装使用)  Linux 环境 发行版:CentOS 7_x64 安 ...