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 一.总效果 本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动 ...
随机推荐
- Memcached 笔记与总结(4)memcache 扩展的使用
在 wamp 环境下进行测试:WAMPSERVER 2.2(Windows 7 + Apache 2.2.21 + PHP 5.3.10 + memcache 3.0.8 + Memcached 1. ...
- Unity3d中C#使用指针(Unsafe)的办法(转)
近日由于在U3D项目中要使用到数据传递(C++ DLL的数据传递给U3D中的C#),其中涉及到需要使用C#的指针.直接编译会出现以下错误Unsafe code requires the 'unsafe ...
- wordpress页面前端添加编辑按钮
<?php edit_post_link(__('Edit This')); ?> 在single.php或者page.php模板页面加入以上代码片段.当管理员登录后,可以直接点击编辑文章 ...
- BAT批处理(一)
本文摘自博文<BAT批处理文件教程> 这是一篇技术教程,我会用很简单的文字表达清楚自己的意思,只要你识字就能看懂,就能学到知识.写这篇教程的目的,是让每一个看过这些文字的朋友记住一句话:如 ...
- maven库文件所在目录
C:\Documents and Settings\jgzhang2\.m2\repository
- Greedy_algorithm
https://en.wikipedia.org/wiki/Greedy_algorithm
- BLE Device Monitor的使用
1 综述 BLE Device Monitor是一个用来显示任意蓝牙低功耗设备服务(services).特征(characteristics).属性(attributes)的windows程序.除了测 ...
- 编写category时的便利宏(用于解决category方法从静态库中加载需要特别设置的问题)
代码摘录自YYKit:https://github.com/ibireme/YYKit /** Add this macro before each category implementation, ...
- php--tp3.2引入sphinx搜索
1.首先我们把coreseek下载好,命名为coreseek,我们找到coreseek/etc中的csft_mysql.conf修改这个配置文件 #源定义 source lemai { type ...
- 设计模式:中介者模式(Mediator)
定 义:用一个中介对象来封装一系列对象的交互.中介者使各个对象不需要显示地相互作用,从而耦合松散,而且可以独立的改变他们之间的交互. 结构图: Mediator类,抽象中介者类 abstract ...