// 初始化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;
}
}


 

最新文章

  1. 十种MYSQL显错注入原理讲解(二)
  2. Remoting&amp;WebService的区别之处
  3. Android开发-无法新建Activity及新建后编译错误
  4. 不错的TOMCAT监控好工具probe
  5. Android中如何查看内存(上)
  6. nginx添加nginx_mod_h264_streaming-2.2.7模块编译报错
  7. Mvc分页:为IQueryable定义一个扩展方法,直接反回PagedList&lt;T&gt;结果集
  8. Drupal 实战
  9. Swift中文教程(二)--简单值
  10. flask 真是太棒啦,阅读手册后就能做出一个博客了
  11. Tencent研发工程师笔试知识点
  12. 我也来写spring
  13. oracle中查询用户信息
  14. 【转】SQL数据库日志文件收缩
  15. Linux c使用gumbo库解析页面表单信息(一)
  16. Virtualbox 虚拟机安装Linux
  17. 「SDOI2014」Lis 解题报告
  18. Eclipse下生成/编辑Java类图或时序图(UML)[转载]
  19. Confluence 6 使用 Apache 和 mod_proxy 添加 SSL和其他
  20. Spark机器学习中ml和mllib中矩阵、向量

热门文章

  1. 大数据开发实战:Hive优化实战2-大表join小表优化
  2. Android -- setWillNotDraw()
  3. 文本相似度-BM25算法
  4. mahout0.7 示例运行纪实
  5. echarts文档对照
  6. OpenNebula学习第三节之虚拟机管理
  7. Ant编译utf-8非法字符:/65279 解决方法
  8. installers PHPManager
  9. 快讯:微软安卓版个人助理(Cortana)在美国境内进行公測
  10. selenium之 chromedriver与chrome版本映射表(更新至v2.38)