自定义UITabbarcontrollerview
2013-06-24 10:17 甘超波 阅读(2410) 评论(0) 编辑 收藏 举报 // 初始化contentView
[self initContentView];
#pragma mark 初始化contentView - (void)initContentView { CGSize size = self.view.bounds.size; UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height - kTabbarHeight)]; contentView.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1]; [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(0, 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 = 0; index < count; index++) { MJTabbarItemDesc *desc = [items objectAtIndex:index]; CGFloat itemX = index * itemWidth; CGRect itemFrame = CGRectMake(itemX, 0, 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) { // 让第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:10]; } return self; } #pragma mark - 覆盖父类的2个方法 #pragma mark 设置按钮标题的frame - (CGRect)titleRectForContentRect:(CGRect)contentRect { UIImage *image = [self imageForState:UIControlStateNormal]; CGFloat titleY = image.size.height - 3; CGFloat titleHeight = self.bounds.size.height - titleY; return CGRectMake(0, titleY, self.bounds.size.width, titleHeight); } #pragma mark 设置按钮图片的frame - (CGRect)imageRectForContentRect:(CGRect)contentRect { UIImage *image = [self imageForState:UIControlStateNormal]; return CGRectMake(0, 0, 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:0]; 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(0, 0, 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(0, 0, 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; } }
目前我正在专注NLP,请立刻加微信/QQ号 546611623, 免费送你原创《NLP高级执行师》高清视频