自定义UITabbarcontrollerview
// 初始化contentView
[self initContentView];
#pragma mark 初始化contentView
- (void)initContentView {
CGSize size = self.view.bounds.size;
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(, , size.width, size.height - kTabbarHeight)];
contentView.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:];
[self.view addSubview:contentView];
[contentView release];
_contentView = contentView;
}
// 设置主题
[self initTheme];
#pragma mark 设置主题
- (void)initTheme {
// 设置导航栏背景
UIImage *navBg = [UIImage resizeImage:@"navigationbar_background.png"];
[[UINavigationBar appearance] setBackgroundImage:navBg forBarMetrics:UIBarMetricsDefault]; // 设置导航栏文字属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
// 设置文字颜色
[attrs setObject:[UIColor blackColor] forKey:UITextAttributeTextColor];
[[UINavigationBar appearance] setTitleTextAttributes:attrs];
}
// 初始化所有的子控制器
[self initControllers];
#pragma mark 初始化子控制器
- (void)initControllers {
_items = [NSMutableArray array]; HomeController *home = [[HomeController alloc] init];
[self addController:home title:@"首页" normal:@"tabbar_home.png" highlighted:@"tabbar_home_highlighted.png"];
[home release]; MyDataController *data = [[MyDataController alloc] init];
[self addController:data title:@"我的资料" normal:@"tabbar_message_center.png" highlighted:@"tabbar_message_center_highlighted.png"];
[data release]; FriendController *friend = [[FriendController alloc] init];
[self addController:friend title:@"我的关注" normal:@"tabbar_profile.png" highlighted:@"tabbar_profile_highlighted.png"];
[friend release]; FollowerController *follower = [[FollowerController alloc] init];
[self addController:follower title:@"我的粉丝" normal:@"tabbar_discover.png" highlighted:@"tabbar_discover_highlighted.png"];
[follower release]; UIViewController *more = [[UIViewController alloc] init];
more.view.backgroundColor = [UIColor redColor];
[self addController:more title:@"更多" normal:@"tabbar_more.png" highlighted:@"tabbar_more_highlighted.png"];
[more release];
}
=========================================
#pragma mark 添加子控制器和item
- (void)addController:(UIViewController *)vc title:(NSString *)title normal:(NSString *)normal highlighted:(NSString *)highlighted {
// 初始化item
MJTabbarItemDesc *item = [MJTabbarItemDesc itemWithTitle:title normal:normal highlighted:highlighted];
[_items addObject:item]; // 设置控制器的标题
vc.title = title; // 包装控制器
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:vc]; nav1.delegate = self; // 对子控制器做了一次retain
[self addChildViewController:nav1];
[nav1 release];
}
// 初始化tabbar
[self initTabbar];
#pragma mark - 私有方法
#pragma mark 初始化tabbar
- (void)initTabbar {
CGSize size = self.view.bounds.size;
CGRect frame = CGRectMake(, size.height - kTabbarHeight, size.width, kTabbarHeight);
MJTabbar *tab = [[MJTabbar alloc] initWithFrame:frame items:_items];
// 设置代理
tab.delegate = self;
[self.view addSubview:tab];
[tab release]; _tabbar = tab;
} ============================================
// 自定义Tabbar #import <UIKit/UIKit.h> @protocol MJTabbarDelegate <NSObject>
- (void)tabbarItemChangeFrom:(int)from to:(int)to;
@end @interface MJTabbar : UIView
// items : 有多少个标签
- (id)initWithFrame:(CGRect)frame items:(NSArray *)items; @property (nonatomic, assign) id<MJTabbarDelegate> delegate;
@end #import "MJTabbar.h"
#import "MJTabbarItem.h" @interface MJTabbar() {
// 当前被选中的tabbaritem
MJTabbarItem *_current;
}
@end @implementation MJTabbar #pragma mark item点击
- (void)itemClick:(MJTabbarItem *)new {
// 设置selected为YES,就能达到UIControlStateSelected状态
if (_current != new) {
if ([self.delegate respondsToSelector:@selector(tabbarItemChangeFrom:to:)]) {
[self.delegate tabbarItemChangeFrom:_current.tag to:new.tag];
} _current.userInteractionEnabled = YES;
new.userInteractionEnabled = NO; new.selected = YES;
_current.selected = NO; _current = new;
}
} #pragma mark 构造方法
- (id)initWithFrame:(CGRect)frame items:(NSArray *)items {
if (self = [super initWithFrame:frame]) {
// colorWithPatternImage : 平铺一张图片来生成背景颜色
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background.png"]]; int count = items.count;
CGFloat itemHeight = self.bounds.size.height;
CGFloat itemWidth = self.bounds.size.width / count; for (int index = ; index < count; index++) {
MJTabbarItemDesc *desc = [items objectAtIndex:index];
CGFloat itemX = index * itemWidth;
CGRect itemFrame = CGRectMake(itemX, , itemWidth, itemHeight);
MJTabbarItem *item = [[MJTabbarItem alloc] initWithFrame:itemFrame itemDesc:desc]; // 设置一个标记
item.tag = index; [item addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:item];
[item release]; if (index == ) {
// 让第0个item选中
[self itemClick:item];
}
} }
return self;
} @end 自定义导航按钮
#import <UIKit/UIKit.h>
@class MJTabbarItemDesc; @interface MJTabbarItem : UIButton
- (id)initWithFrame:(CGRect)frame itemDesc:(MJTabbarItemDesc *)desc;
@end @interface MJTabbarItemDesc : NSObject
@property (nonatomic, copy) NSString *title; // 标题
@property (nonatomic, copy) NSString *normal; //默认图标
@property (nonatomic, copy) NSString *highlighted; // 高亮图标 + (id)itemWithTitle:(NSString *)title normal:(NSString *)normal highlighted:(NSString *)highlighted;
@end #import "MJTabbarItem.h" @implementation MJTabbarItem - (id)initWithFrame:(CGRect)frame itemDesc:(MJTabbarItemDesc *)desc {
if (self = [super initWithFrame:frame]) {
// 设置高亮显示的背景
[self setHighlightedBg:@"tabbar_slider.png"];
// 设置selected=YES时的背景
[self setSelectedBg:@"tabbar_slider.png"]; // 设置默认的Image
[self setImage:[UIImage imageNamed:desc.normal] forState:UIControlStateNormal];
// 设置selected=YES时的image
[self setImage:[UIImage imageNamed:desc.highlighted] forState:UIControlStateSelected]; // 不需要在用户长按的时候调整图片为灰色
self.adjustsImageWhenHighlighted = NO;
// 设置UIImageView的图片居中
self.imageView.contentMode = UIViewContentModeCenter; // 设置文字
[self setTitle:desc.title forState:UIControlStateNormal];
// 设置文字居中
self.titleLabel.textAlignment = NSTextAlignmentCenter;
// 设置字体大小
self.titleLabel.font = [UIFont systemFontOfSize:];
}
return self;
} #pragma mark - 覆盖父类的2个方法
#pragma mark 设置按钮标题的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
UIImage *image = [self imageForState:UIControlStateNormal];
CGFloat titleY = image.size.height - ;
CGFloat titleHeight = self.bounds.size.height - titleY;
return CGRectMake(, titleY, self.bounds.size.width, titleHeight);
}
#pragma mark 设置按钮图片的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
UIImage *image = [self imageForState:UIControlStateNormal];
return CGRectMake(, , self.bounds.size.width, image.size.height);
} @end @implementation MJTabbarItemDesc
+ (id)itemWithTitle:(NSString *)title normal:(NSString *)normal highlighted:(NSString *)highlighted {
MJTabbarItemDesc *desc = [[MJTabbarItemDesc alloc] init];
desc.title = title;
desc.normal = normal;
desc.highlighted = highlighted;
return [desc autorelease];
} - (void)dealloc {
[_title release];
[_normal release];
[_highlighted release];
[super dealloc];
}
@end
tabbar和导航控制用代理的交互
#pragma mark - MJTabbar的代理方法
// 在这里切换子控制器
- (void)tabbarItemChangeFrom:(int)from to:(int)to {
// 取出旧控制器
UIViewController *old = [self.childViewControllers objectAtIndex:from];
// 移除旧控制器的view
[old.view removeFromSuperview]; // 取出新控制器
UIViewController *new = [self.childViewControllers objectAtIndex:to];
new.view.frame = _contentView.bounds;
// 添加新控制器的view
[_contentView addSubview:new.view]; // 执行动画
if (from != to) {
CATransition *anim = [CATransition animation];
anim.duration = 0.15;
anim.type = kCATransitionPush;
anim.subtype = (to > from)?kCATransitionFromRight:kCATransitionFromLeft;
[_contentView.layer addAnimation:anim forKey:nil];
}
}
导航控制器代理方法
#pragma mark 返回
- (void)back {
[_currentController.navigationController popViewControllerAnimated:YES];
} #pragma mark home
- (void)home {
[_currentController.navigationController popToRootViewControllerAnimated:YES];
} #pragma mark - 导航控制器代理方法
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UIViewController *root = [navigationController.viewControllers objectAtIndex:];
if (viewController == root) {
// 回到根控制器,显示tabbar
[UIView animateWithDuration:0.3 animations:^{
// 显示tabbar
CGRect frame = _tabbar.frame;
frame.origin.y = self.view.bounds.size.height - _tabbar.bounds.size.height;
_tabbar.frame = frame; // 改变contentView的高度
frame = _contentView.frame;
frame.size.height = _tabbar.frame.origin.y;
_contentView.frame = frame;
}]; } else {
// 左边的返回按键
UIButton *btn = [[UIButton alloc] init];
UIImage *back = [UIImage imageNamed:@"navigationbar_back.png"];
[btn setBackgroundImage:back forState:UIControlStateNormal];
UIImage *back2 = [UIImage imageNamed:@"navigationbar_back_highlighted.png"];
[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundImage:back2 forState:UIControlStateHighlighted];
btn.bounds = CGRectMake(, , back.size.width, back.size.height); UIBarButtonItem *left = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
viewController.navigationItem.leftBarButtonItem = left; // 右边的home按键
btn = [[UIButton alloc] init];
UIImage *home = [UIImage imageNamed:@"navigationbar_home.png"];
[btn setBackgroundImage:home forState:UIControlStateNormal];
UIImage *home2 = [UIImage imageNamed:@"navigationbar_home_highlighted.png"];
[btn addTarget:self action:@selector(home) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundImage:home2 forState:UIControlStateHighlighted];
btn.bounds = CGRectMake(, , home.size.width, home.size.height); UIBarButtonItem *right = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
viewController.navigationItem.rightBarButtonItem = right; // 跳到其他控制器,隐藏tabbar
[UIView animateWithDuration:0.3 animations:^{
// 隐藏tabbar
CGRect frame = _tabbar.frame;
frame.origin.y = self.view.bounds.size.height;
_tabbar.frame = frame; // 改变contentView的高度
frame = _contentView.frame;
frame.size.height = self.view.bounds.size.height;
_contentView.frame = frame;
}]; _currentController = viewController;
}
}
自定义UITabbarcontrollerview的更多相关文章
- ios总结目录
:iOS中er二维码的使用 http://www.cnblogs.com/gcb999/p/3183655.html :iOS中根据数据自动生成有规律的(UItextField和UILabel) IO ...
- 关于Unity3D自定义编辑器的学习
被人物编辑器折腾了一个月,最终还是交了点成品上去(还要很多优化都还么做). 刚接手这项工作时觉得没概念,没想法,不知道.后来就去看<<Unity5.X从入门到精通>>中有关于 ...
- 一起学微软Power BI系列-使用技巧(5)自定义PowerBI时间日期表
1.日期函数表作用 经常使用Excel或者PowerBI,Power Pivot做报表,时间日期是一个重要的纬度,加上做一些钻取,时间日期函数表不可避免.所以今天就给大家分享一个自定义的做日期表的方法 ...
- JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome
今天为大家分享一下我自己制作的浏览器滚动条,我们知道用css来自定义滚动条也是挺好的方式,css虽然能够改变chrome浏览器的滚动条样式可以自定义,css也能够改变IE浏览器滚动条的颜色.但是css ...
- ASP.NET Aries 入门开发教程8:树型列表及自定义右键菜单
前言: 前面几篇重点都在讲普通列表的相关操作. 本篇主要讲树型列表的操作. 框架在设计时,已经把树型列表和普通列表全面统一了操作,用法几乎是一致的. 下面介绍一些差距化的内容: 1:树型列表绑定: v ...
- ASP.NET Aries 入门开发教程5:自定义列表页工具栏区
前言: 抓紧时间,继续写教程,因为发现用户期待的内容,都在业务处理那一块. 不得不继续勤劳了. 这节主要介绍工具栏区的玩法. 工具栏的默认介绍: 工具栏默认包括5个按钮,根据不同的权限决定显示: 添加 ...
- UWP中实现自定义标题栏
UWP中实现自定义标题栏 0x00 起因 在UWP开发中,有时候我们希望实现自定义标题栏,例如在标题栏中加入搜索框.按钮之类的控件.搜了下资料居然在一个日文网站找到了一篇介绍这个主题的文章: http ...
- JavaScript 自定义对象
在Js中,除了Array.Date.Number等内置对象外,开发者可以通过Js代码创建自己的对象. 目录 1. 对象特性:描述对象的特性 2. 创建对象方式:对象直接量.new 构造函数.Objec ...
- 【WCF】自定义错误处理(IErrorHandler接口的用法)
当被调用的服务操作发生异常时,可以直接把异常的原始内容传回给客户端.在WCF中,服务器传回客户端的异常,通常会使用 FaultException,该异常由这么几个东东组成: 1.Action:在服务调 ...
随机推荐
- Mac-OSX下Ruby更新
Mac下是自带Ruby环境的,在有些情况我们是需要更新Ruby的,安装和更新Ruby环境可以通过rvm命令进行操作,rvm在安装过程中通过HomeBrew安装依赖包,如果之前没有装过HomeBrew, ...
- Springboot读取自定义配置文件的几种方法
一.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. ...
- TNTSearch 轻量级全文索引 + 中文分词
TNTSearch 轻量级全文索引+中文分词 选用 TNTSearch 的原因:轻,方便移植,不需要额外安装服务,能减少后期维护的工作量.搜索的效果也还不错,可以满足大多数项目场景,如果对性能和精准度 ...
- Python在VSCode中进入交互界面调试
VSCode非常强大,断点好用,美中不足,每次只能通过下面窄窄一行进行各种检查,而python的优点就在于交互式的调试,所以希望能够在断点时能够进入到正常的交互界面进行调试. 我用的插件是: 设置交互 ...
- (转)真正的中国天气api接口xml,json(求加精) ...
我只想说现在网上那几个api完全坑爹有木有??? 官方的申请不来有木有,还有收费有木有?? 咱这种菜鸟只能用免费的了!!!! http://m.weather.com.cn/data/101110 ...
- 【Python】torrentParser1.02
#------------------------------------------------------------------------------------ # torrentParse ...
- git 本地与服务器的同步
如果在服务器上直接用 git init 则会自动在当前目录创建.git目录但在本地clone后修改后,不能push,这是由于git默认拒绝了push操作,需要进行设置,修改.git/config添加如 ...
- Perl、PHP、Python、Java 和 Ruby 比较【转载+整理】
从本文的内容上,写的时间比较早,而且有些术语我认为也不太准,有点口语化,但是意思到了. 问题: Perl.Python.Ruby 和 PHP 各自有何特点? 为什么动态语言多作为轻量级的解决方案? L ...
- LintCode: Binary Tree Postorder Traversal
C++,递归 /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *righ ...
- ORA-01654错误
问题现象: 测试库使用如下方式创建索引: create index IDX_ANA_OFFICE on ANA (OFFICE_CITY, OFFICE_NO) tablespace IDX ...