自定义的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 ...
随机推荐
- 84.Largest Rectangle in histogram---stack
题目链接:https://leetcode.com/problems/largest-rectangle-in-histogram/description/ 题目大意:在直方图中找出最大的矩形面积.例 ...
- HDU 6205 2017沈阳网络赛 思维题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6205 题意:给你n堆牌,原本每一堆的所有牌(a[i]张)默认向下,每次从第一堆开始,将固定个数的牌(b ...
- linux系统查找具体进程
ps -ef | grep '查找内容' eg:ps -ef | grep '测试USB设备穿透'
- Xcode及模拟器SDK下载
http://blog.csdn.net/zhangao0086/article/details/38491271 吐槽下,百度打着无限分享的旗号,却又让分享资源过期,让分享者持续维护 如果你嫌在Ap ...
- 转 proc文件
/proc 是一个伪文件系统, 被用作内核数据结构的接口, 而不仅仅是解释说明/dev/kmem. /proc 里的大多数文件都是只读的, 但也可以通过写一些文件来改变内核变量. 下面对整个 /pro ...
- naive cube implementation in python
这篇论文中提到的naive cube算法的实现,python写出来真的就和伪代码差不多=.= 输入大约长这样,依次是 index userid country state city topic cat ...
- Tornado入门资料整理
预备知识 没学过计网的苦逼找点现成一些的东西看吧…… <Restful Web Services>,<HTTP The Definitive Guide>,各种RFC WSGI ...
- IEEEXtreme 10.0 - Mysterious Maze
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mysterious Maze 题目来源 第10届IEEE极限编程大赛 https://www.hacker ...
- MVC Partial页面的使用
先建立Action: public PartialViewResult CurrentCount() { ViewBag.Count = CurrentUserCount; return Partia ...
- 使用FlashPaper在线转换.doc为.swf_实用技巧
https://yq.aliyun.com/ziliao/160044?spm=5176.8246799.0.0.JBbqjY 摘要: 本文讲的是使用FlashPaper在线转换.doc为.swf_实 ...