首先创建一个类,继承自UItabBarController

然后在.m文件中:

这里我有两个宏定义:

#define WIDTH (myView.frame.size.width / 4) //我在写4个按钮(即4个标签,所以是所有宽度除以4)
#define HEIGHT (myView.frame.size.height)

然后写延展,声明了一个按钮属性,用来记录上一个被点击的按钮(这是用来改变颜色,即点击状态的):

@interface MyTabBarController ()

//之前选中的按钮
@property(nonatomic, retain) UIButton *selectedBtn; @end

然后开始写自己想要的东西了,在viewDidLoad中:

(1)删除继承父类而来的tabBar,自定义自己想要的视图,其frame为原来tabBar的frame。

(2)然后定义了4个按钮和4个label,即标签和标签下的标题

(3)然后循环给4个按钮添加点击事件,触发事件是同一个clickBtn

- (void)viewDidLoad {
[super viewDidLoad];
//删除现有的tabBar
CGRect rect = self.tabBar.frame;
[self.tabBar removeFromSuperview]; //添加自己的视图
UIView *myView = [[UIView alloc] init];
myView.backgroundColor = [UIColor whiteColor];
myView.frame = rect;
[self.view addSubview:myView]; //主页发现按钮
UIButton *findBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[findBtn setImage:[UIImage imageNamed:@"icon_home_n"] forState:UIControlStateNormal];
[findBtn setImage:[UIImage imageNamed:@"icon_home_h"] forState:UIControlStateSelected];
findBtn.tag = 1;
findBtn.frame = CGRectMake(0, -20, WIDTH, HEIGHT);
//默认选中是发现按钮
findBtn.selected = YES;
[findBtn setTintColor:[UIColor colorWithRed:0.886 green:0.365 blue:0.247 alpha:1.000]];
self.selectedBtn = findBtn;
[myView addSubview:findBtn]; UILabel *findLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)];
findLabel.text = @"发现";
findLabel.textColor = [UIColor colorWithRed:0.804 green:0.325 blue:0.227 alpha:1.000];
findLabel.font = [UIFont systemFontOfSize:10];
findLabel.tag = 101;
[myView addSubview:findLabel];
[findLabel release]; //搜索页
UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[searchBtn setImage:[UIImage imageNamed:@"icon_search_n"] forState:UIControlStateNormal];
[searchBtn setImage:[UIImage imageNamed:@"icon_search_h"] forState:UIControlStateSelected];
searchBtn.tag = 2;
searchBtn.frame = CGRectMake(WIDTH, -20, WIDTH, HEIGHT);
[myView addSubview:searchBtn]; UILabel *searchLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH + WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)];
searchLabel.text = @"搜索";
searchLabel.textColor = [UIColor grayColor];
searchLabel.font = [UIFont systemFontOfSize:10];
searchLabel.tag = 102;
[myView addSubview:searchLabel];
[searchLabel release]; //收藏页
UIButton *costBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[costBtn setImage:[UIImage imageNamed:@"iconfont-like"] forState:UIControlStateNormal];
[costBtn setImage:[UIImage imageNamed:@"iconfont-like-selected"] forState:UIControlStateSelected];
costBtn.tag = 3;
costBtn.frame = CGRectMake(2 * WIDTH, -20, WIDTH, HEIGHT);
[myView addSubview:costBtn]; UILabel *collectLabel = [[UILabel alloc] initWithFrame:CGRectMake(2 * WIDTH + WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)];
collectLabel.text = @"收藏";
collectLabel.textColor = [UIColor grayColor];
collectLabel.font = [UIFont systemFontOfSize:10];
collectLabel.tag = 103;
[myView addSubview:collectLabel];
[collectLabel release]; //设置页
UIButton *setUpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[setUpBtn setImage:[UIImage imageNamed:@"icon_setting_n"] forState:UIControlStateNormal];
[setUpBtn setImage:[UIImage imageNamed:@"icon_setting_h"] forState:UIControlStateSelected];
setUpBtn.tag = 4;
setUpBtn.frame = CGRectMake(3 * WIDTH, -20, WIDTH, HEIGHT);
[myView addSubview:setUpBtn]; UILabel *setUpLabel = [[UILabel alloc] initWithFrame:CGRectMake(3 * WIDTH + WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)];
setUpLabel.text = @"设置";
setUpLabel.textColor = [UIColor grayColor];
setUpLabel.font = [UIFont systemFontOfSize:10];
setUpLabel.tag = 104;
[myView addSubview:setUpLabel];
[setUpLabel release]; for (int i = 1; i <4; i++) { //4个按钮的点击事件是一个
UIButton *btn = [self.view viewWithTag:i];
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
}
}

(4)然后实现按钮的点击事件clickBtn:实现按钮和其下面标题 选择和未选择状态的切换;也需要实现视图的跳转:

#pragma mark -- 自定义tabBar按钮的点击事件
-(void)clickBtn:(UIButton *)sender {
//设置上一个按钮
self.selectedBtn.selected = NO;
UILabel *titleFormer = [self.view viewWithTag:self.selectedBtn.tag + 100];
titleFormer.textColor = [UIColor grayColor]; //设置选中状态
sender.selected = YES;
self.selectedBtn = sender;
UILabel *titleNow = [self.view viewWithTag:self.selectedBtn.tag + 100];
titleNow.textColor = [UIColor colorWithRed:0.804 green:0.325 blue:0.227 alpha:1.000]; //跳转相应的视图控制器
self.selectedIndex = sender.tag - 1;
}

然后就基本实现了一个视图控制器的功能了,自己还有什么需求可以往里加,使用就跟视图的标签视图一样使用就行,只是这里的按钮图片我都是在类里就赋值好了;

这是我自定义的tabBarController下载地址,需要的可以下下来参考参考:

http://download.csdn.net/detail/margaret_mo/9451753

自定义UITabBarController标签视图控制器的更多相关文章

  1. UITabBarController — 标签视图控制器

    UITabBarController - 标签视图控制器 UITabBarController 分为三层结构: (1).tab bar (2.)Custom Content (3.). Tab bar ...

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

    直接上代码: // // AppDelegate.m // // #import "AppDelegate.h" #import "RootViewController. ...

  3. 标签视图控制器UITabBarController

    标签视图控制器 UITabBarController FirstViewController*first = [[FirstViewController alloc] init]; //创建一个UIT ...

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

    目录:[Swift]Xcode实际操作 本文将为你演示,选项卡视图控制器的创建和使用. 在项目文件夹[DemoApp]上点击鼠标右键,弹出右键菜单. [New File]->[Cocoa Tou ...

  5. iOS学习22之视图控制器

    1.自定义视图 1> 概述   定义视图:系统标准UI之外,自己组合而出的新的视图. 定义视图的优点: iOS提供了很多UI组件,借助它们我们可以实现不同的功能.尽管如此,实际开发中,我们还需要 ...

  6. 集合视图控制器(CollectionViewController) 、 标签控制器(TabBarController) 、 高级控件介绍

    1 创建集合视图,设置相关属性以满足要求 1.1 问题 集合视图控制器UIConllectionViewController是一个展示大量数据的控制器,系统默认管理着一个集合视图UICollectio ...

  7. ##DAY3 自定义视图、视图控制器、视图控制器指定视图、loadView、 viewDidLoad、MVC、屏幕旋转、内存警告

    ##DAY3 自定义视图.视图控制器.视图控制器指定视图.loadView. viewDidLoad.MVC.屏幕旋转.内存警告 #pragma mark ———————自定义视图的步骤 —————— ...

  8. Swift - 标签条(UITabBar)标签页控制器(UITabBarController)用法

    App底部的tab标签页可以方便的把功能模块划分清楚,只需点击相应的标签页就可以展示完全独立的视图页面,同时各标签页间的视图也可以进行数据交换.   TabBarItem系统自带图标样式(System ...

  9. 自定义视图控制器切换(iOS)

    在iOS开发过程中,通常我们会使用UINavigationController,UITabbarController等苹果提供的视图控制器来切换我们的视图.在iOS5之前,如果要自定义容器视图控制器很 ...

随机推荐

  1. mono for android学习过程系列教程(4)

    今天要讲的事情是构建安卓程序的UI界面. 首先给大家上点小点心,如图: 上面就是我们界面的设计模块,仔细看中间大块的下方,有一个Source,这就类似webform里面的设计和源代码界面. 在这个页面 ...

  2. ASP.NET MVC 视图(五)

    ASP.NET MVC 视图(五) 前言 上篇讲解了视图中的分段概念.和分部视图的使用,本篇将会对Razor的基础语法简洁的说明一下,前面的很多篇幅中都有涉及到视图的调用,其中用了很多视图辅助器,也就 ...

  3. AngularJs之三

    一.angularJs的指令模型ng-model指令 ng-model 指令 绑定 HTML 元素 到应用程序数据. 为应用程序数据提供类型验证(number.email.required).为应用程 ...

  4. ASP.NET中Session的sessionState 4种mode模式

    1. sessionState的4种mode模式 在ASP.NET中Session的sessionState的4中mode模式:Off.InProc.StateServer及SqlServer. 2. ...

  5. 【WCF】操作选择器

    在开始吹牛之前,先说说.net Core的事情. 你不能把.NET Core作为全新体系来学习,因为它也是.NET.关于.NET Core,老周并不打算写什么,因为你懂了.NET,就懂了.NET Co ...

  6. 【.net 深呼吸】使用二进制格式来压缩XML文档

    在相当多的情况下,咱们写入XML文件默认是使用文本格式来写入的,如果XML内容是通过网络传输,或者希望节省空间,特别是对于XML文档较大的情况,是得考虑尽可能地压缩XML文件的大小. XmlDicti ...

  7. Python模拟登陆新浪微博

    上篇介绍了新浪微博的登陆过程,这节使用Python编写一个模拟登陆的程序.讲解与程序如下: 1.主函数(WeiboMain.py): import urllib2 import cookielib i ...

  8. Event事件

    妙味课堂-Event事件 1.焦点:当一个元素有焦点的时候,那么他就可以接受用户的输入(不是所有元素都能接受焦点) 给元素设置焦点的方式: 1.点击 2.tab 3.js 2.(例子:输入框提示文字) ...

  9. 浅析MySQL复制

    MySQL的复制是基于binlog来实现的. 流程如下 涉及到三个线程,主库的DUMP线程,从库的IO线程和SQL线程. 1. 主库将所有操作都记录到binlog中.当复制开启时,主库的DUMP线程根 ...

  10. PHP的学习--在Atom中使用XDebug(Mac)

    之前写过一篇博客<PHP的学习--在sublime中使用XDebug(Ubuntu)>,讲了在Ubuntu系统 sublime 中配置 XDebug,其实配置好之后,我也很少用,原因有两点 ...