网上大多的自己定义TabBar都是继承View的,项目中要用到path+Tabbat这种话(path用的MMDrawerController这个框架),继承View的Tabbar是无法满足条件的(不是一个容器)。以下就介绍下自己写的一个自己定义TabBar。

和系统自带的写法差点儿相同,先看下原生的写法例如以下:

首先在.h中自己定义一个UITabBarController* tabBarViewController; 接着在.m中来做实现,

tabBarViewController = [[UITabBarController alloc]init];

    FirstViewController* first = [[FirstViewController alloc]init];
UINavigationController * navOne = [[UINavigationController alloc] initWithRootViewController:first ];
[navOne setNavigationBarHidden:TRUE]; SecondViewController* second = [[SecondViewController alloc]init];
UINavigationController * navtwo = [[UINavigationController alloc] initWithRootViewController:second ];
[navtwo setNavigationBarHidden:TRUE]; ViewController3 *vc3 = [[ViewController3 alloc] init];
UINavigationController * nav3 = [[UINavigationController alloc] initWithRootViewController:vc3 ];
[nav3 setNavigationBarHidden:TRUE];
ViewController5 *vc5 = [[ViewController5 alloc] init];
UINavigationController * nav5 = [[UINavigationController alloc] initWithRootViewController:vc5 ];
[nav5 setNavigationBarHidden:TRUE]; tabBarViewController.viewControllers = [NSArray arrayWithObjects:navOne, navtwo,nav3,nav5, nil];
[first release];
[second release];
[vc3 release];
[vc5 release]; UITabBar *tabBar = tabBarViewController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3]; tabBarItem1.title = @"消息";
tabBarItem2.title = @"应用";
tabBarItem3.title = @"通讯录";
tabBarItem4.title = @"我"; [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"home_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"maps_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"maps.png"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"myplan_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"myplan.png"]];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"settings_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"settings.png"]];
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]]; [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor greenColor];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, UITextAttributeTextColor,
nil] forState:UIControlStateSelected];

以下来看下自己定义的方法,也是在.h文件里定义

UITabBarController* tabBarViewController;

在定义一个long preSelect变量,这个变量用来推断点击是哪个selectedIndex。

在.m中的详细实现例如以下:

 NSMutableArray *arrController = [[NSMutableArray alloc]init];
//信息
MessageViewController *messageVC = [[MessageViewController alloc]initWithNibName:@"MessageViewController" bundle:nil]; UINavigationController * navMessage = [[UINavigationController alloc] initWithRootViewController:messageVC ];
[messageVC release]; [navMessage setNavigationBarHidden:TRUE];
[arrController addObject:navMessage];
[navMessage release];
//应用
AppsViewController *appsVC = [[AppsViewController alloc]initWithNibName:@"AppsViewController" bundle:nil]; UINavigationController *navApps = [[UINavigationController alloc] initWithRootViewController:appsVC];
[appsVC release];
[navApps setNavigationBarHidden:TRUE];
[arrController addObject:navApps];
[navApps release];
//通讯录
AddressViewController *addressVC = [[AddressViewController alloc]initWithNibName:@"AddressViewController" bundle:nil]; UINavigationController *navAddress = [[UINavigationController alloc] initWithRootViewController:addressVC];
[addressVC release];
[navAddress setNavigationBarHidden:TRUE];
[arrController addObject:navAddress];
[navAddress release];
//我
MeViewController *meVC = [[MeViewController alloc]initWithNibName:@"MeViewController" bundle:nil]; UINavigationController *navMe = [[UINavigationController alloc] initWithRootViewController:meVC];
[meVC release];
[navMe setNavigationBarHidden:TRUE];
[arrController addObject:navMe];
[navMe release]; tabBarViewController=[[UITabBarController alloc] init]; tabBarViewController.viewControllers=arrController;
[arrController release];
preSelect = 1;
for (int i = 0; i<4; i++) {
NSString *nalString = [NSString stringWithFormat:@"tabbar_unselecte%d.png", i+1];
NSString *highString = [NSString stringWithFormat:@"tabbar_selected%d.png", i+1];
UIImage *nalImage = [UIImage imageNamed:nalString];
UIImage *highImage = [UIImage imageNamed:highString]; UIButton *tabbarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
tabbarBtn.frame = CGRectMake(i*(320/4), 0, (320/4), 46);
[tabbarBtn addTarget:self action:@selector(tabbarBtnPress:) forControlEvents:UIControlEventTouchUpInside];
[tabbarBtn setBackgroundImage:nalImage forState:UIControlStateNormal];
[tabbarBtn setBackgroundImage:highImage forState:UIControlStateSelected];
tabbarBtn.tag = 100+i;
[tabBarViewController.tabBar addSubview:tabbarBtn]; if (i == 1) {
tabbarBtn.selected = YES;
tabBarViewController.selectedIndex=1;
}
}

主要是思路就是自己定义UITabBarController的tabBar属性,在它上面addSubview。因为加的UIButton,如今就要实现点击

方法来切换。

方法例如以下:

-(void)tabbarBtnPress:(UIButton *)tabbarBtn
{
if (preSelect != tabbarBtn.tag -100 ) { UIButton *prvBtn = (UIButton *)[tabbarBtn.superview viewWithTag:preSelect+100];
prvBtn.selected = NO; tabBarViewController.selectedIndex = preSelect = tabbarBtn.tag-100; UIButton *button = (UIButton *)[tabbarBtn.superview viewWithTag:tabbarBtn.tag];
button.selected = YES;
}
}

到此就完毕了。有什么不正确地方请大家指出来。

5.29日补充:

上面写的是默认是选中第2个tabbar,选中第一个时。仅仅须要改动preSelect =0;和

if(i==0)

{

tabbarBtn.selected =YES;

self.selectedIndex=0;

}


假设想把这个独立出来,仅仅须要定义一个继承UITabBarController的类。

假设想用自己定义的背景也能够 。能够把系统的给隐藏(self.tabBar.hidden =YES;),自己定义一个View,加到tabbar上。[self.view addSubview:xxxx];
,其他还是用上面的方法。

6.3:

发现了一个问题。VC的nav不显示,找了下原因,发现 tabbarController.viewControllers 中设置是VC数组,不是nav数组。

自己定义UITabBarController的更多相关文章

  1. iOS开发——UI篇Swift篇&UITabBarController

    UITabBarController class UITabBarControllerController: UIViewController { var titleString:String! @I ...

  2. iOS-一个弹出菜单动画视图开源项目分享

    相似于Tumblr公布button的弹出视图 使用非常easy: 初始化: @property (nonatomic, strong) XWMenuPopView *myMenuPopView; - ...

  3. 超文本传输协议-HTTP/1.1

    超文本传输协议-HTTP/1.1(修订版) ---译者:孙超进本协议不限流传发布.版权声明Copyright (C) The Internet Society (1999). All Rights R ...

  4. Snail—UI学习之自己定义标签栏UITabBarController

    这里的背景跟上面的差点儿相同 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkF ...

  5. IOS总结_无需自己定义UITabbar也可改变UITabbarController的背景和点击和的颜色

    在application: application didFinishLaunchingWithOptions: launchOptions 增加以下代码就能够实现对tabbar的颜色的改动 //设定 ...

  6. 自定义UITabBarController标签视图控制器

    首先创建一个类,继承自UItabBarController 然后在.m文件中: 这里我有两个宏定义: #define WIDTH (myView.frame.size.width / 4) //我在写 ...

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

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

  8. 你真的了解UITabBarController吗?

    一:首先查看一下关于UITabBarController的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewCo ...

  9. iOS学习28之UITabBarController

    1. 标签视图控制器 -- UITabBarController 视图(UIView) ---> 图层 ---> 子视图 视图控制器(UIViewController) ---> 管 ...

随机推荐

  1. activity变成Dialog的样式设置

    1.属性解析 <item name="android:windowFrame">@null</item> :Dialog的windowFrame框为无< ...

  2. Search a 2D Matrix【python】

    class Solution: # @param matrix, a list of lists of integers # @param target, an integer # @return a ...

  3. winform 之1---窗体介绍

    窗体是winform开发的基础,需要掌握窗体的创建.属性.调用和窗体传值等等. 1.在项目中添加窗体很简单,在项目上点击右键,选择添加windows窗体即可为项目添加新的窗体. 2.窗体的属性设置,右 ...

  4. javascript每日一练(九)——运动一:匀速运动

    一.js的运动 匀速运动 清除定时器 开启定时器 运动是否完成:a.运动完成,清除定时器:b.运动未完成继续 匀速运动停止条件:距离足够近  Math.abs(当然距离-目标距离) < 最小运动 ...

  5. 使用VS+VisualGDB编译Linux版本RCF(相当于Linux也有COM版本了)

    阅读目录 通过向导配置项目 配置目录结构 修改项目配置 添加RCF源代码 完成配置并进行编译 添加测试程序 添加测试代码——通过TCP进行通信 运行测试程序并查看测试结果 VisualGDB生成的所有 ...

  6. Python 获取时间戳

    Python 获取时间通过 time 模块 如下代码,是通过获取当前的时间,按照格式输出 Python默认获取当前的时间返回的都是时间的元组,下面是元组的,字符串时间的一个转换输出 # -*- cod ...

  7. 在Windows上使用CodeLite+MinGW+Clang进行开发

    前几天听说clang 3.4已经release了,然后我又手痒就折腾一下,在这里记录一下折腾的经过. 在以前就试过clang-cl+VC的开发环境,编译代码到是没发现什么大问题,有不少警告而已,不过c ...

  8. Windows Phone 8初学者开发的翻译终于过半

    从2013年7月19日开始,到2013年12月9日,一共花了143天时间完成了18篇Windows Phone 8初学者开发的翻译,还剩下17篇文章需要翻译,看到了完成的希望! I love Wind ...

  9. java BigDecimal的使用和四舍五入及格式规范(精准数据)

    • Java中的简单浮点数类型float和double不能够进行运算.不光是Java,在其它很多编程语言中也有这样的问题. 如果我们编译运行下面这个程序会看到什么? public   class  T ...

  10. 动态绑定Gridview带模板列

    公司要做一个可以支持4种数据库(<!--数据库类型 (DLL专用) (SQL SERVER) (ORACLE) (ACCESS)(POSTGRE SQL)-->)的并且字段随表字段变化的可 ...