iOS 自定义UITabBarController的tabBar
- #import <UIKit/UIKit.h>
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
- @property (strong, nonatomic) UIWindow *window;
- @end
- /**
- * 素材图片的链接: http://pan.baidu.com/s/1geahYRT 密码: axmh
- * 注意图片的尺寸,否则会变形
- */
- #import "AppDelegate.h"
- #import "FirstViewController.h"
- #import "SecondViewController.h"
- #import "ThirdViewController.h"
- #import "FourthViewController.h"
- @interface AppDelegate ()<UITabBarControllerDelegate>
- {
- float imgWidth;//tabBarController的宽度
- UITabBarController *_tabBarController;
- NSMutableArray *imageViewArr;
- }
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- self.window.backgroundColor = [UIColor whiteColor];
- //初始化数组
- imageViewArr = [[NSMutableArray alloc] init];
- //初始化控制器
- FirstViewController *firstVC = [[FirstViewController alloc] init];
- SecondViewController *secondVC = [[SecondViewController alloc] init];
- ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
- FourthViewController *fourthVC = [[FourthViewController alloc] init];
- NSArray *array = @[firstVC,secondVC,thirdVC,fourthVC];
- //初始化UITabBarController
- _tabBarController = [[UITabBarController alloc] init];
- //背景图片
- [_tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"1.png"]];
- //默认选中第一个
- _tabBarController.selectedIndex = ;
- //设置代理
- _tabBarController.delegate = self;
- _tabBarController.viewControllers = array;
- imgWidth = [UIScreen mainScreen].bounds.size.width/(array.count*1.0);
- //初始化tabBarController
- [self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"11.png" selectedImageViewTag: selectedImage:@"111.png" tabBarItemTitle:@"首页"];
- [self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"12.png" selectedImageViewTag: selectedImage:@"112.png" tabBarItemTitle:@"导航"];
- [self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"13.png" selectedImageViewTag: selectedImage:@"113.png" tabBarItemTitle:@"消息"];
- [self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"14.png" selectedImageViewTag: selectedImage:@"114.png" tabBarItemTitle:@"更多"];
- //设置文字的颜色
- [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:UITextAttributeTextColor] forState:];
- //设置选中时文字的颜色
- [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor] forState:UIControlStateSelected];
- self.window.rootViewController = _tabBarController;
- [self.window makeKeyAndVisible];
- return YES;
- }
- /**
- * 初始化tabBarController的tabBar
- *
- * @param frame 图片的尺寸
- * @param imageName 底层图片的名字
- * @param tag 图片的标签
- * @param selectedImageName 顶层图片的名字
- * @param title 标题
- */
- - (void)setupView:(CGRect)frame image:(NSString *)imageName selectedImageViewTag:(int)tag selectedImage:(NSString *)selectedImageName tabBarItemTitle:(NSString*)title{
- //底层图片
- UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
- imgView.image = [UIImage imageNamed:imageName];
- //顶层图片(不选中时隐藏)
- UIImageView *selectedImageView = [[UIImageView alloc] initWithFrame:frame];
- selectedImageView.tag = tag;
- selectedImageView.image = [UIImage imageNamed:selectedImageName];
- if ((tag-) == ) {
- selectedImageView.hidden = NO;
- }else{
- selectedImageView.hidden = YES;
- }
- [_tabBarController.tabBar addSubview:imgView];
- [_tabBarController.tabBar addSubview:selectedImageView];
- UITabBar *tabBar = _tabBarController.tabBar;
- UITabBarItem *tabBarItem = [tabBar.items objectAtIndex:(tag - )];
- //设置标题
- tabBarItem.title = title;
- //selectedImageView存放到数组
- [imageViewArr addObject:selectedImageView];
- }
- #pragma mark -- UITabBarControllerDelegate的方法 --
- - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
- //遍历imageView,选中的就不隐藏,其它的隐藏
- for (UIImageView *imageView in imageViewArr) {
- if (imageView.tag == +tabBarController.selectedIndex) {
- imageView.hidden = NO;
- }else{
- imageView.hidden = YES;
- }
- }
- }
- @end
- #import <UIKit/UIKit.h>
- @interface FirstViewController : UIViewController
- @end
- #import "FirstViewController.h"
- @interface FirstViewController ()
- @end
- @implementation FirstViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor redColor];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #import <UIKit/UIKit.h>
- @interface SecondViewController : UIViewController
- @end
- #import "SecondViewController.h"
- @interface SecondViewController ()
- @end
- @implementation SecondViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor orangeColor];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
- #import <UIKit/UIKit.h>
- @interface ThirdViewController : UIViewController
- @end
- #import "ThirdViewController.h"
- @interface ThirdViewController ()
- @end
- @implementation ThirdViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor yellowColor];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
- #import <UIKit/UIKit.h>
- @interface FourthViewController : UIViewController
- @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
- @end
iOS 自定义UITabBarController的tabBar的更多相关文章
- iOS 自定义滑动切换TabBar
貌似经常会用到,自己整理收藏起来,方便日后查找备用. 效果如图: 由于制作gif,调整了属性,所以看起来的效果不好.如果用默认配置,生成的gif会很大. 制作gif: 1.使用QuickTimePla ...
- iOS-自定义 UITabBarController
先来回顾一下UITabBarController ( 稍微详细的在在http://blog.csdn.net/yang198907/article/details/49807011) 伴随UITabB ...
- 自定义UITabBarController
用的时候直接拷贝代码即可. 1.在AppDelegate设置跟控制器为:PQTabBarController #import "PQTabBarController.h" @int ...
- 【iOS自定义键盘及键盘切换】详解
[iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...
- iOS自定义的UISwitch按钮
UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...
- 如何实现 iOS 自定义状态栏
给大家介绍如何实现 iOS 自定义状态栏 Sample Code: 01 UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIAp ...
- 自定义UITabbarController控制器
自定义UITabbarController控制器 这是定制UITabbarController的基本原理,没有进行功能性封装. 效果: 源码地址: https://github.com/YouXi ...
- iOS自定义组与组之间的距离以及视图
iOS自定义组与组之间的距离以及视图 //头视图高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...
- iOS 自定义转场动画
代码地址如下:http://www.demodashi.com/demo/12955.html 一.总效果 本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动 ...
随机推荐
- ps命令交叉编译
busybox中的ps命令是针对于嵌入式的,其中一些选项并不完整.因此需要将源码下载下来,进行交叉编译 官方下载地址 github下载地址 含有configure,我在此使用的是这个源码包,官方的包在 ...
- web接入层 传入参数的格式化及web返回值传出数据的参数格式化,都要统一
1.web接入层 传入参数的格式化及web返回值传出数据的参数格式化,都要统一. 比如acSpace中, 传入层参数@RequestBody javaBean对象.统一转换为javabean传入参数. ...
- 个人翻译的cedec2010基于物理的光照
作为自己介绍基于物理渲染计划的一部分,在自己总结和发布的同时,也会翻译一些国外的优秀资料做推广 本文是Tri Ace 在 cedec2010上发布的文章,主要描述了他们基于物理光照的实现方法,这 ...
- php防攻击方法
php防攻击方法 更多答案 请参考 @如何有效防止XSS攻击/AJAX跨域攻击 我说下防止非法用户的一些常用手段吧 1 前端的js验证: 我认为js验证只是一种用户体验的提升,对普通用户群体的简单 ...
- XML解析器(转)
常见C/C++ XML解析器有tinyxml.XERCES.squashxml.xmlite.pugxml.libxml等等,这些解析器有些是支持多语言的,有些只是单纯C/C++的.如果你是第一次接触 ...
- Spark 2.0
Apache Spark 2.0: Faster, Easier, and Smarter http://blog.madhukaraphatak.com/categories/spark-two/ ...
- php mysql 事务处理
MYSQL 的事务处理主要有两种方法. 1 .用 begin,rollback,commit 来实现 begin 开始一个事务 rollback 事务回滚 commit 事务确认 2 .直接用 ...
- UML 关系
1. 关联关系(association) 关联关系式是用一条直线表示的,如A—B.表示在一段时间内将多个类的实例连接在一起,关联关系描述了某个对象在一段时间内一直知道另一个对象的存在.在Rose中为了 ...
- getComputedStyle()与currentStyle
getComputedStyle()与currentStyle计算元素样式 发表于 2011-10-27 由 admin “DOM2级样式”增强了document.defaultView,提供了get ...
- 第四章 跨平台图像显示库——SDL 第一节 与SDL第一次亲密接触
http://blog.csdn.net/visioncat/article/details/1596576 GCC for Win32 开发环境介绍(5) 第四章 跨平台图像显示库——SDL 第一节 ...