自定义的tabBarController的几种方法
本文转载自:http://blog.sina.com.cn/s/blog_79c5bdc30100t88i.html
#import <UIKit/UIKit.h>
@interface CustomTabBarViewController : UITabBarController {
UIImage *_tabBarBackgroundImage; //整个tabBar的背景
NSMutableArray *_unSelectedImageArray; //非选中效果的tabBarItem数组
NSMutableArray *_selectedImageArray; //选中效果的tabBarItem数组
NSMutableArray *_itemBgImageViewArray; //item背景UIIMageView数组
int _lastSelectedIndex; //上一次选中的tabBarItem的index
int _hiddenIndex;
}
@property (nonatomic, retain) UIImage *tabBarBackgroundImage;
@property (nonatomic, retain) NSMutableArray *unSelectedImageArray;
@property (nonatomic, retain) NSMutableArray *selectedImageArray;
@property (nonatomic, retain) NSMutableArray *itemBgImageViewArray;
@property (nonatomic, assign) int lastSelectedIndex;
@property (nonatomic, assign) int hiddenIndex;
- (id)initWithTabBarBackgroundImage:(UIImage *)barBackgroundImage
unSelectedImageArray:(NSMutableArray *)unImageArray
selectedImageArray:(NSMutableArray *)imageArray;
//隐藏某个tabBarItem的图片
-(void)hiddeItemImageView:(int)index;
//显示某个tabBarItem的图片
-(void)showItemImageView:(int)index;
@end
#import "CustomTabBarViewController.h"
#define TabBarBackgroundImageViewTag 11111
@implementation CustomTabBarViewController
@synthesize tabBarBackgroundImage = _tabBarBackgroundImage;
@synthesize unSelectedImageArray = _unSelectedImageArray;
@synthesize selectedImageArray = _selectedImageArray;
@synthesize itemBgImageViewArray = _itemBgImageViewArray;
@synthesize lastSelectedIndex = _lastSelectedIndex;
@synthesize hiddenIndex = _hiddenIndex;
- (void)dealloc
{
self.tabBarBackgroundImage = nil;
self.unSelectedImageArray = nil;
self.selectedImageArray = nil;
self.itemBgImageViewArray = nil;
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
self.tabBarBackgroundImage = nil;
}
- (id)initWithTabBarBackgroundImage:(UIImage *)barBackgroundImage
unSelectedImageArray:(NSMutableArray *)unImageArray
selectedImageArray:(NSMutableArray *)imageArray {
self = [super init];
if (self) {
// self.tabBarBackgroundImage = barBackgroundImage;
// self.unSelectedImageArray = unImageArray;
// self.selectedImageArray = imageArray;
//
// self.itemBgImageViewArray = [NSMutableArray array];
// _lastSelectedIndex = 0;
// _hiddenIndex = -1;
}
return self;
}
- (id)init {
self = [super init];
if (self) {
// self.tabBarBackgroundImage = [UIImage imageNamed:@"menu_bg.png"];
//
// NSMutableArray *aunSelectedImageArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"menu_paiyou.png"],
// [UIImage imageNamed:@"menu_chat.png"],
// [UIImage imageNamed:@"menu_map.png"],
// [UIImage imageNamed:@"menu_renwu.png"],
// [UIImage imageNamed:@"menu_huibao.png"], nil];
// self.unSelectedImageArray = aunSelectedImageArray;
// [aunSelectedImageArray release];
//
// NSMutableArray *aselectedImageArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"menu_paiyou_pressed.png"],
// [UIImage imageNamed:@"menu_chat_pressed.png"],
// [UIImage imageNamed:@"menu_map_pressed.png"],
// [UIImage imageNamed:@"menu_renwu_pressed.png"],
// [UIImage imageNamed:@"menu_huibao_pressed.png"], nil];
// self.selectedImageArray = aselectedImageArray;
// [aselectedImageArray release];
//
// self.itemBgImageViewArray = [NSMutableArray array];
// _lastSelectedIndex = 0;
// _hiddenIndex = -1;
}
return self;
}
#pragma mark - itemIndex methods
- (void)setLastSelectedIndex:(int)lastSelectedIndex {
if (_lastSelectedIndex != lastSelectedIndex) {
//将上次的选中效果取消
UIImageView *lastSelectedImageView = (UIImageView *)[_itemBgImageViewArray objectAtIndex:_lastSelectedIndex];;
lastSelectedImageView.image = [_unSelectedImageArray objectAtIndex:_lastSelectedIndex];
_lastSelectedIndex = lastSelectedIndex;
}
}
- (void)setSelectedIndex:(NSUInteger)selectedIndex {
[super setSelectedIndex:selectedIndex];
//将上次的选中效果取消
self.lastSelectedIndex = selectedIndex;
//将本次的选中效果显示
UIImageView *selectedImageView = (UIImageView *)[_itemBgImageViewArray objectAtIndex:selectedIndex];
selectedImageView.image = [_selectedImageArray objectAtIndex:selectedIndex];
}
//隐藏某个tabBarItem的图片
- (void)hiddeItemImageView:(int)index {
if (_hiddenIndex != index) {
_hiddenIndex = index;
UIImageView *hiddenImageView = (UIImageView *)[_itemBgImageViewArray objectAtIndex:_hiddenIndex];
hiddenImageView.hidden = YES;
}
}
//显示某个tabBarItem的图片
- (void)showItemImageView:(int)index {
if (_hiddenIndex == index) {
UIImageView *hiddenImageView = (UIImageView *)[_itemBgImageViewArray objectAtIndex:_hiddenIndex];
hiddenImageView.hidden = NO;
_hiddenIndex = -1;
}
}
#pragma mark - View lifecycle
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
[super loadView];
self.tabBarBackgroundImage = [UIImage imageNamed:@"menu_bg.png"];
NSMutableArray *aunSelectedImageArray = [[NSMutableArray alloc] initWithObjects:[UIImageimageNamed:@"menu_paiyou.png"],
[UIImage imageNamed:@"menu_chat.png"],
[UIImage imageNamed:@"menu_map.png"],
[UIImage imageNamed:@"menu_renwu.png"],
[UIImage imageNamed:@"menu_huibao.png"], nil];
self.unSelectedImageArray = aunSelectedImageArray;
[aunSelectedImageArray release];
NSMutableArray *aselectedImageArray = [[NSMutableArray alloc] initWithObjects:[UIImageimageNamed:@"menu_paiyou_pressed.png"],
[UIImage imageNamed:@"menu_chat_pressed.png"],
[UIImage imageNamed:@"menu_map_pressed.png"],
[UIImage imageNamed:@"menu_renwu_pressed.png"],
[UIImage imageNamed:@"menu_huibao_pressed.png"], nil];
self.selectedImageArray = aselectedImageArray;
[aselectedImageArray release];
self.itemBgImageViewArray = [NSMutableArray array];
_lastSelectedIndex = 0;
_hiddenIndex = -1;
}
#define ItemWidth 50
#define ItemHeight 49
#define SideMarginX 7
#define SideMarginY 0.5
#define Spacing 14
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *tabBarBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,self.tabBar.frame.size.width, self.tabBar.frame.size.height)];
tabBarBackgroundImageView.tag = TabBarBackgroundImageViewTag;
tabBarBackgroundImageView.contentMode = UIViewContentModeScaleAspectFit;
tabBarBackgroundImageView.image = _tabBarBackgroundImage;
[self.tabBar insertSubview:tabBarBackgroundImageView atIndex:0];
[tabBarBackgroundImageView release];
for (int i = 0; i < 5; i++) {
UIImageView *itemBg = [[UIImageView alloc] initWithFrame:CGRectMake(SideMarginX +ItemWidth * i + Spacing * i, SideMarginY, ItemWidth, ItemHeight)];
itemBg.contentMode = UIViewContentModeScaleAspectFit;
itemBg.image = [_unSelectedImageArray objectAtIndex:i];
[self.tabBar insertSubview:itemBg atIndex:1];
[_itemBgImageViewArray addObject:itemBg];
[itemBg release];
}
self.selectedIndex = 0;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.itemBgImageViewArray = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - UITabBarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
self.selectedIndex = [tabBar.items indexOfObject:item];
}
@end
方法二:http://www.cocoachina.com/bbs/read.php?tid-62061-page-1.html
自定义的tabBarController的几种方法的更多相关文章
- WPF编程,使用WindowChrome实现自定义窗口功能的一种方法。
原文:WPF编程,使用WindowChrome实现自定义窗口功能的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/arti ...
- WPF编程,自定义鼠标形状的一种方法。
原文:WPF编程,自定义鼠标形状的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/details/8727 ...
- Ajax设置自定义请求头的两种方法
用自定义请求头token为例 方法一 $.ajax({ type: "post", url:"http://127.0.0.1:4564/bsky-app/templat ...
- python中自定义超时异常的几种方法
最近在项目中调用第三方接口时候,经常会出现请求超时的情况,或者参数的问题导致调用异代码异常.针对超时异常,查询了python 相关文档,没有并发现完善的包来根据用户自定义的时间来抛出超时异常的模块.所 ...
- springmvc跳转到自定义404页面的三种方法
有时候我们并不想跳转到系统自定义的错误页面中,那么我们需要自定义页面并且实现它的跳转 有三种方法可以实现 方法一:最简单的实现,也是最快的 在<web-app>节点下配置 代码如下: &l ...
- Chrome模拟手机浏览器(iOS/Android)的三种方法,亲测无误!
大网站都有推出自己的手机访问版本页面,不管是新闻类还是视频网站,我们在电脑是无法直接访问到手机网站的,比如我经常访问一个3g.qq.com这个手机站点,如果在电脑上直接打开它,则会跳转到其它页面,一般 ...
- 用Fiddler可以设置浏览器的UA 和 手动 --Chrome模拟手机浏览器(iOS/Android)的三种方法,亲测无误!
附加以一种软件的方法是:用Fiddler可以设置浏览器的UA 以下3种方法是手动的 通过伪装User-Agent,将浏览器模拟成Android设备. 第一种方法:新建Chrome快捷方式 右击桌面上的 ...
- 关于iOS自定义UITabBar的几种方法
作为iOS开发最常用的两个多视图控制器 NavigationController 和 TabBarController 已经很强大了,基本上在大部分的应用中都能看到它们的影子.但是在使用的过程中,系统 ...
- iOS 中使用 XIB 自定义cell 的两种方法 以及 编译出现常见 的错误 ++++(xcode6.0之后)
一. 注册cell 1.创建自定义cell并勾选 xib :(勾选xib就会自动生成与cell文件关联的xib) 2.在 tableViewController里注册自定义Cell (或者遵守tabl ...
随机推荐
- C# Selenium with PhantomJSDriver get image width and height (获取图片的长和高)
//get image width and height var image=driver.FindElement(By.ClassName("it-Header_authorImage&q ...
- 大数据系列之分布式数据库HBase-1.2.4+Zookeeper 安装及增删改查实践
之前介绍过关于HBase 0.9.8版本的部署及使用,本篇介绍下最新版本HBase1.2.4的部署及使用,有部分区别,详见如下: 1. 环境准备: 1.需要在Hadoop[hadoop-2.7.3] ...
- acm专题---键树
题目来源:http://hihocoder.com/problemset/problem/1014?sid=982973 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms ...
- chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息
chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig [--ad ...
- AGC 16 D - XOR Replace
AGC 16 D - XOR Replace 附上attack(自为风月马前卒爷) 的题解 Problem Statement There is a sequence of length N: a=( ...
- hdu 3573(数学+贪心)
Buy Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Sublime Text 2之Emmet插件安装及使用
1.安装Emmet How To Install?Reffer to this link:http://www.ituring.com.cn/article/47310 2.使用Emmet--Abbr ...
- 洛谷P1420 最长连号 题解
题目传送门 这道题我是打暴力的...(尴尬) 所以直接是O(N2)的时间,但好像没有炸,数据很水... #include<bits/stdc++.h> using namespace st ...
- currentTime安卓
设定一个时间编写CurrentTime类设置属性为该时间用toString显示该时间我使用的currentTime ,苹果用起来是好使得为什么.安卓走到下面这一步却不接着走呢!!! e.current ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) F - Uniformly Branched Trees 无根树->有根树+dp
F - Uniformly Branched Trees #include<bits/stdc++.h> #define LL long long #define fi first #de ...