1. #import <UIKit/UIKit.h>
  2.  
  3. @interface AppDelegate : UIResponder <UIApplicationDelegate>
  4.  
  5. @property (strong, nonatomic) UIWindow *window;
  6.  
  7. @end
  1. /**
  2. * 素材图片的链接: http://pan.baidu.com/s/1geahYRT 密码: axmh
  3. * 注意图片的尺寸,否则会变形
  4. */
  5. #import "AppDelegate.h"
  6. #import "FirstViewController.h"
  7. #import "SecondViewController.h"
  8. #import "ThirdViewController.h"
  9. #import "FourthViewController.h"
  10.  
  11. @interface AppDelegate ()<UITabBarControllerDelegate>
  12. {
  13. float imgWidth;//tabBarController的宽度
  14. UITabBarController *_tabBarController;
  15. NSMutableArray *imageViewArr;
  16. }
  17. @end
  18.  
  19. @implementation AppDelegate
  20.  
  21. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  22. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  23. // Override point for customization after application launch.
  24. self.window.backgroundColor = [UIColor whiteColor];
  25.  
  26. //初始化数组
  27. imageViewArr = [[NSMutableArray alloc] init];
  28. //初始化控制器
  29. FirstViewController *firstVC = [[FirstViewController alloc] init];
  30. SecondViewController *secondVC = [[SecondViewController alloc] init];
  31. ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
  32. FourthViewController *fourthVC = [[FourthViewController alloc] init];
  33. NSArray *array = @[firstVC,secondVC,thirdVC,fourthVC];
  34. //初始化UITabBarController
  35. _tabBarController = [[UITabBarController alloc] init];
  36. //背景图片
  37. [_tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"1.png"]];
  38. //默认选中第一个
  39. _tabBarController.selectedIndex = ;
  40. //设置代理
  41. _tabBarController.delegate = self;
  42. _tabBarController.viewControllers = array;
  43. imgWidth = [UIScreen mainScreen].bounds.size.width/(array.count*1.0);
  44. //初始化tabBarController
  45. [self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"11.png" selectedImageViewTag: selectedImage:@"111.png" tabBarItemTitle:@"首页"];
  46. [self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"12.png" selectedImageViewTag: selectedImage:@"112.png" tabBarItemTitle:@"导航"];
  47. [self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"13.png" selectedImageViewTag: selectedImage:@"113.png" tabBarItemTitle:@"消息"];
  48. [self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"14.png" selectedImageViewTag: selectedImage:@"114.png" tabBarItemTitle:@"更多"];
  49. //设置文字的颜色
  50. [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:UITextAttributeTextColor] forState:];
  51. //设置选中时文字的颜色
  52. [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor] forState:UIControlStateSelected];
  53.  
  54. self.window.rootViewController = _tabBarController;
  55. [self.window makeKeyAndVisible];
  56. return YES;
  57. }
  58. /**
  59. * 初始化tabBarController的tabBar
  60. *
  61. * @param frame 图片的尺寸
  62. * @param imageName 底层图片的名字
  63. * @param tag 图片的标签
  64. * @param selectedImageName 顶层图片的名字
  65. * @param title 标题
  66. */
  67. - (void)setupView:(CGRect)frame image:(NSString *)imageName selectedImageViewTag:(int)tag selectedImage:(NSString *)selectedImageName tabBarItemTitle:(NSString*)title{
  68. //底层图片
  69. UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
  70. imgView.image = [UIImage imageNamed:imageName];
  71. //顶层图片(不选中时隐藏)
  72. UIImageView *selectedImageView = [[UIImageView alloc] initWithFrame:frame];
  73. selectedImageView.tag = tag;
  74. selectedImageView.image = [UIImage imageNamed:selectedImageName];
  75. if ((tag-) == ) {
  76. selectedImageView.hidden = NO;
  77. }else{
  78. selectedImageView.hidden = YES;
  79. }
  80. [_tabBarController.tabBar addSubview:imgView];
  81. [_tabBarController.tabBar addSubview:selectedImageView];
  82. UITabBar *tabBar = _tabBarController.tabBar;
  83. UITabBarItem *tabBarItem = [tabBar.items objectAtIndex:(tag - )];
  84. //设置标题
  85. tabBarItem.title = title;
  86. //selectedImageView存放到数组
  87. [imageViewArr addObject:selectedImageView];
  88. }
  89. #pragma mark -- UITabBarControllerDelegate的方法 --
  90. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
  91. //遍历imageView,选中的就不隐藏,其它的隐藏
  92. for (UIImageView *imageView in imageViewArr) {
  93. if (imageView.tag == +tabBarController.selectedIndex) {
  94. imageView.hidden = NO;
  95. }else{
  96. imageView.hidden = YES;
  97. }
  98. }
  99. }
  100.  
  101. @end
  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface FirstViewController : UIViewController
  4.  
  5. @end
  1. #import "FirstViewController.h"
  2.  
  3. @interface FirstViewController ()
  4.  
  5. @end
  6.  
  7. @implementation FirstViewController
  8.  
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. self.view.backgroundColor = [UIColor redColor];
  12. }
  13.  
  14. - (void)didReceiveMemoryWarning {
  15. [super didReceiveMemoryWarning];
  16. // Dispose of any resources that can be recreated.
  17. }
  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface SecondViewController : UIViewController
  4.  
  5. @end
  1.  
  1. #import "SecondViewController.h"
  2.  
  3. @interface SecondViewController ()
  4.  
  5. @end
  6.  
  7. @implementation SecondViewController
  8.  
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. self.view.backgroundColor = [UIColor orangeColor];
  12. }
  13.  
  14. - (void)didReceiveMemoryWarning {
  15. [super didReceiveMemoryWarning];
  16. // Dispose of any resources that can be recreated.
  17. }
  18.  
  19. @end
  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface ThirdViewController : UIViewController
  4.  
  5. @end
  1. #import "ThirdViewController.h"
  2.  
  3. @interface ThirdViewController ()
  4.  
  5. @end
  6.  
  7. @implementation ThirdViewController
  8.  
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. self.view.backgroundColor = [UIColor yellowColor];
  12. }
  13.  
  14. - (void)didReceiveMemoryWarning {
  15. [super didReceiveMemoryWarning];
  16. // Dispose of any resources that can be recreated.
  17. }
  18.  
  19. @end
  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface FourthViewController : UIViewController
  4.  
  5. @end

#import "FourthViewController.h"

@interface FourthViewController ()

@end

@implementation FourthViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor greenColor];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

  1. @end

iOS 自定义UITabBarController的tabBar的更多相关文章

  1. iOS 自定义滑动切换TabBar

    貌似经常会用到,自己整理收藏起来,方便日后查找备用. 效果如图: 由于制作gif,调整了属性,所以看起来的效果不好.如果用默认配置,生成的gif会很大. 制作gif: 1.使用QuickTimePla ...

  2. iOS-自定义 UITabBarController

    先来回顾一下UITabBarController ( 稍微详细的在在http://blog.csdn.net/yang198907/article/details/49807011) 伴随UITabB ...

  3. 自定义UITabBarController

    用的时候直接拷贝代码即可. 1.在AppDelegate设置跟控制器为:PQTabBarController #import "PQTabBarController.h" @int ...

  4. 【iOS自定义键盘及键盘切换】详解

    [iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...

  5. iOS自定义的UISwitch按钮

    UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...

  6. 如何实现 iOS 自定义状态栏

    给大家介绍如何实现 iOS 自定义状态栏 Sample Code: 01 UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIAp ...

  7. 自定义UITabbarController控制器

    自定义UITabbarController控制器 这是定制UITabbarController的基本原理,没有进行功能性封装. 效果:   源码地址: https://github.com/YouXi ...

  8. iOS自定义组与组之间的距离以及视图

    iOS自定义组与组之间的距离以及视图 //头视图高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...

  9. iOS 自定义转场动画

    代码地址如下:http://www.demodashi.com/demo/12955.html 一.总效果 本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动 ...

随机推荐

  1. ps命令交叉编译

    busybox中的ps命令是针对于嵌入式的,其中一些选项并不完整.因此需要将源码下载下来,进行交叉编译 官方下载地址 github下载地址 含有configure,我在此使用的是这个源码包,官方的包在 ...

  2. web接入层 传入参数的格式化及web返回值传出数据的参数格式化,都要统一

    1.web接入层 传入参数的格式化及web返回值传出数据的参数格式化,都要统一. 比如acSpace中, 传入层参数@RequestBody javaBean对象.统一转换为javabean传入参数. ...

  3. 个人翻译的cedec2010基于物理的光照

    作为自己介绍基于物理渲染计划的一部分,在自己总结和发布的同时,也会翻译一些国外的优秀资料做推广    本文是Tri Ace 在 cedec2010上发布的文章,主要描述了他们基于物理光照的实现方法,这 ...

  4. php防攻击方法

    php防攻击方法   更多答案 请参考 @如何有效防止XSS攻击/AJAX跨域攻击 我说下防止非法用户的一些常用手段吧 1 前端的js验证: 我认为js验证只是一种用户体验的提升,对普通用户群体的简单 ...

  5. XML解析器(转)

    常见C/C++ XML解析器有tinyxml.XERCES.squashxml.xmlite.pugxml.libxml等等,这些解析器有些是支持多语言的,有些只是单纯C/C++的.如果你是第一次接触 ...

  6. Spark 2.0

    Apache Spark 2.0: Faster, Easier, and Smarter http://blog.madhukaraphatak.com/categories/spark-two/ ...

  7. php mysql 事务处理

    MYSQL 的事务处理主要有两种方法. 1 .用 begin,rollback,commit 来实现 begin 开始一个事务 rollback 事务回滚 commit 事务确认    2 .直接用  ...

  8. UML 关系

    1. 关联关系(association) 关联关系式是用一条直线表示的,如A—B.表示在一段时间内将多个类的实例连接在一起,关联关系描述了某个对象在一段时间内一直知道另一个对象的存在.在Rose中为了 ...

  9. getComputedStyle()与currentStyle

    getComputedStyle()与currentStyle计算元素样式 发表于 2011-10-27 由 admin “DOM2级样式”增强了document.defaultView,提供了get ...

  10. 第四章 跨平台图像显示库——SDL 第一节 与SDL第一次亲密接触

    http://blog.csdn.net/visioncat/article/details/1596576 GCC for Win32 开发环境介绍(5) 第四章 跨平台图像显示库——SDL 第一节 ...