#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. ul li横向排列及圆点处理

    如何用CSS制作横向菜单 让ul li横向排列及圆点处理   第一步:建立一个无序列表 我们先建立一个无序列表,来建立菜单的结构.代码是:<ul> <li><a href ...

  2. NVPerfHUD

    http://www.cnblogs.com/cproom/archive/2006/11/13/559287.html NVPerfHUD是一个很好的3D程序调试工具,它是NVPerfKit的一部分 ...

  3. git 使用钩子直接推送到工作目录

    远端机器 $ mkdir /www/teacherapi  # 创建工作目录 $ cd /data/git $ git init teacherapi.git --bare --shared Init ...

  4. Django 1.8安装使用

    1.使用pip安装django, pip是什么,如何安装?自行放狗搜 # pip install "django<1.9" 2.创建项目 # django-admin sta ...

  5. C#winform项目添加引用编译文件

    打开opencv(Emgu for windows)样例,发现有带引用图标的文件 以前还真没见过这种用法,研究了一下: 1.项目版本信息文件内容及添加: 通过修改项目目录下的csproj(c shar ...

  6. VPN服务器环境搭建

    一.VPN服务器环境说明 操作系统:CentOS release 6.4 (Final) 本地网卡: 复制代码 代码如下: # ifconfig em1 Link encap:Ethernet HWa ...

  7. 蓝牙BLE LINK LAYER剖析(二) -- PDU

    DATA FORMAT The Link Layer has only one packet format used for both advertising channel packets and ...

  8. linux实现c多进程

    线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的Unix也支持线程的概念,但是在一个进程(process)中只允许 ...

  9. Qt from Linux to Windows target

    45down voteaccepted Just use M cross environment (MXE). It takes the pain out of the whole process: ...

  10. CheckedListBoxControl 使用

    绑定用法 StringBuilder sb = new StringBuilder(); sb.AppendFormat("select id, filename, addtime from ...