#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的更多相关文章

  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. li标签行内元素高度及居中

    <head> <title><title> <style type="text/css"> * { padding: 0px; ma ...

  2. ecshop session丢失问题

    ecshop session丢失问题 电子商务PHP 用ecshop搭建了一个电子商务的系统,本地测试一切正常.放到服务器上出现问题: 症状:      点着点着经常无故退出,感觉session被清空 ...

  3. 02/07/2106 @ 6:28am (UTC)

    <?php echo pow(2,32); 4294967296 http://www.unixtimestamp.com/index.php 4294967296 Is equivalent ...

  4. linux下常用的命令

    一.  tomcat  tail -f ../logs/catalina.out                        最新更新的日志(tomcat) cat ../logs/catalina ...

  5. Android之Fragment学习笔记①

    Android Fragment完全解析,关于碎片你所需知道的一切 一. 什么是FragmentFragment(碎片)就是小型的Activity,它是在Android3.0时出现的.Fragment ...

  6. 【转】全面解析Unity3D自动生成的脚本工程文件

    http://blog.csdn.net/jjiss318/article/details/7632041 我们在Unity3D开发的时候,经常会看到它会产生不少固定命名工程文件,诸如: Assemb ...

  7. 超简单的处理JSON格式和JSON数组格式的String

    现在网站上有不少处理JSON格式的工具类,但是我找了一天,发现大都是需要编写相应对象类来进行处理,比较麻烦,比如:Gson,json-lib.Gson,json-lib这些处理那些接口之类的参数名字和 ...

  8. Archiver 浅析

    归档是一个过程,即用某种格式来保存一个或多个对象,以便以后还原这些对象.通常,这个过程包括将(多个)对象写入文件中,以便以后读取该对象. 两种归档数据的方法:属性列表和带键值的编码. 属性列表局限性很 ...

  9. C++内存问题大集合(指针问题,以及字符串拷贝问题,确实挺危险的)

    作者:rendao.org,版权声明,转载必须征得同意. 内存越界,变量被篡改 memset时长度参数超出了数组长度,但memset当时并不会报错,而是操作了不应该操作的内存,导致变量被无端篡改 还可 ...

  10. zepto源码--compact、flatten、camelize、dasherize、uniq--学习笔记

    1.compact 删除数组中的空元素(不是空字符串).undefined.null 在定义变量时,定义过filter = emptyArray.filter,即调用javascript原生的数组处理 ...