// 初始化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的更多相关文章

  1. ios总结目录

    :iOS中er二维码的使用 http://www.cnblogs.com/gcb999/p/3183655.html :iOS中根据数据自动生成有规律的(UItextField和UILabel) IO ...

  2. 关于Unity3D自定义编辑器的学习

    被人物编辑器折腾了一个月,最终还是交了点成品上去(还要很多优化都还么做).  刚接手这项工作时觉得没概念,没想法,不知道.后来就去看<<Unity5.X从入门到精通>>中有关于 ...

  3. 一起学微软Power BI系列-使用技巧(5)自定义PowerBI时间日期表

    1.日期函数表作用 经常使用Excel或者PowerBI,Power Pivot做报表,时间日期是一个重要的纬度,加上做一些钻取,时间日期函数表不可避免.所以今天就给大家分享一个自定义的做日期表的方法 ...

  4. JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome

    今天为大家分享一下我自己制作的浏览器滚动条,我们知道用css来自定义滚动条也是挺好的方式,css虽然能够改变chrome浏览器的滚动条样式可以自定义,css也能够改变IE浏览器滚动条的颜色.但是css ...

  5. ASP.NET Aries 入门开发教程8:树型列表及自定义右键菜单

    前言: 前面几篇重点都在讲普通列表的相关操作. 本篇主要讲树型列表的操作. 框架在设计时,已经把树型列表和普通列表全面统一了操作,用法几乎是一致的. 下面介绍一些差距化的内容: 1:树型列表绑定: v ...

  6. ASP.NET Aries 入门开发教程5:自定义列表页工具栏区

    前言: 抓紧时间,继续写教程,因为发现用户期待的内容,都在业务处理那一块. 不得不继续勤劳了. 这节主要介绍工具栏区的玩法. 工具栏的默认介绍: 工具栏默认包括5个按钮,根据不同的权限决定显示: 添加 ...

  7. UWP中实现自定义标题栏

    UWP中实现自定义标题栏 0x00 起因 在UWP开发中,有时候我们希望实现自定义标题栏,例如在标题栏中加入搜索框.按钮之类的控件.搜了下资料居然在一个日文网站找到了一篇介绍这个主题的文章: http ...

  8. JavaScript 自定义对象

    在Js中,除了Array.Date.Number等内置对象外,开发者可以通过Js代码创建自己的对象. 目录 1. 对象特性:描述对象的特性 2. 创建对象方式:对象直接量.new 构造函数.Objec ...

  9. 【WCF】自定义错误处理(IErrorHandler接口的用法)

    当被调用的服务操作发生异常时,可以直接把异常的原始内容传回给客户端.在WCF中,服务器传回客户端的异常,通常会使用 FaultException,该异常由这么几个东东组成: 1.Action:在服务调 ...

随机推荐

  1. OSAL工作机制分析

    协议栈代码main()函数分析 ZMain文件->ZMain.c->main()  在这里我们重点了解osal_start_system()函数 int main( void ) { // ...

  2. Asp.net Page_ClientValidate 的应用和跳过

    其实网上说道的Page_ClientValidate的博客其实有很多.这里就不列举了,最近在开发遇到一个问题给大家分享一下, 整理后的代码 如下: HTML code, <%@ Page Lan ...

  3. 65. XPages自定义控件(三)高级搜索之三

    RecordView控件的两个文件的完整代码在本文末尾给出.虽说完整,仅靠这两个文件,RecordView控件还不能正常工作,因为在这两个文件里还引用了其他自定义控件,调用了作为managed bea ...

  4. 几种梯度下降方法对比(Batch gradient descent、Mini-batch gradient descent 和 stochastic gradient descent)

    https://blog.csdn.net/u012328159/article/details/80252012 我们在训练神经网络模型时,最常用的就是梯度下降,这篇博客主要介绍下几种梯度下降的变种 ...

  5. 用Hadoop构建电影推荐系统

    转自:http://blog.fens.me/hadoop-mapreduce-recommend/ Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, ...

  6. Linux系统中最好用的截图软件介绍

    当我的主力操作系统从 Windows 转换到 Ubuntu 的时候,首要考虑的就是屏幕截图工具的可用性.尽管使用默认的键盘快捷键也可以获取屏幕截图,但如果使用屏幕截图工具,可以更方便地对屏幕截图进行编 ...

  7. 转: wireshark的使用说明

    原创者:http://www.cnblogs.com/TankXiao/archive/2012/10/10/2711777.html from:  https://mp.weixin.qq.com/ ...

  8. Android 为何比 iOS 卡?【转载】

    Android 卡是必须的,当你的手机装了 20 多个 app,那不卡才叫见鬼了呢,我手机微信都打不开,手机直接自动重启啦~哪种东西生来就是完美的呢?即便是台式机,也是越用越慢.换句话,如果没有特别原 ...

  9. Windows 环境 cygwin 安装 SSH

    本文内容 安装环境 安装 cygwin 安装 SSH 服务 启动 sshd 服务 SSH 免密码登录 验证 SSH 是否已安装成功 验证 SSH 是否可以免密码登录本机 安装环境 Windows 20 ...

  10. beanshell

    http://www.beanshell.org/download.html