上一篇讲到了UITabBarViewController,接着说说UITabBarViewController中怎么自定义TabBar.

今天仿写了微博,发现底部tabbar中间的button和其他有所不同,button较大且着色;而且我们平时工作中也有很多类似这样的问题,有些app有一个看起来像标准 tabBarController,但是呢,tabBar的中间是凸起的或者着色的。我们怎样做可以构建这种现实效果呢?

如图:

这些标签栏除了中间项以外看起来都相当的标准,所以我们会从一个标准的包含一个tabBar的UITabBarController开始;查看应用内的图片,显而易见的是中间的标签是一个简单的自定义button,一个UITabBar 包含了一个UITabBaritems的数组,UITabBaritem继承自UIBarItem。但是和同样继承自UIBarItem的UIBarButtonItem不同,苹果官方没有提供给UITabBarItem创建自定义视图的api。

我们的基本方案是子类化UITabBarController然后添加一个自定义的button再UITabBar上面。

代码实现:(这里我用第二张图片举例)

 //
// TabBarViewController.m
// WeiBo
//
// Created by Oran Wu on 15-11-18.
// Copyright (c) 2015年 Xinxin. All rights reserved.
// #import "TabBarViewController.h"
#import "ViewAdditions.h"
#import "AddViewController.h" @interface TabBarViewController ()<UITabBarControllerDelegate>{ UITabBar *tabBar;
UIImageView *tabBarView;
UIButton *lastSelectedButton; UILabel *titleLabel; } @end @implementation TabBarViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. //把原tabBar隐藏
self.tabBar.hidden = YES; //在底部创建一个tabBarView替代原tabBar
tabBarView =[[UIImageView alloc] initWithFrame:(CGRect){,self.view.height-,self.view.width,}];
[self.view addSubview:tabBarView];
//可交互
tabBarView.userInteractionEnabled = YES; //创建常见的四个tabBarButton
[self creatButtonWithNormalName:@"tabbar_home" andSelectedName:@"tabbar_home_selected" andTitle:@"首页" andIndex:];
[self creatButtonWithNormalName:@"tabbar_message_center" andSelectedName:@"tabbar_message_center_selected" andTitle:@"消息" andIndex:];
[self creatButtonWithNormalName:@"tabbar_discover" andSelectedName:@"tabbar_discover_selected" andTitle:@"发现" andIndex:];
[self creatButtonWithNormalName:@"tabbar_profile" andSelectedName:@"tabbar_profile_selected" andTitle:@" 我" andIndex:];
[self creatCenterButton:@"jia"]; //这里用了通知,切换页面时用来隐藏tabBar
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideTabbar:) name:@"HideTabbar" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showTabbar) name:@"ShowTapBar" object:nil];
} - (void)hideTabbar:(NSNotification *)notification{
// NSNumber *number = notification.object; tabBarView.hidden = YES;
} - (void)showTabbar{ tabBarView.hidden = NO;
} //自定义方法用来设置button两种状态的图片
- (void)creatButtonWithNormalName:(NSString*)normal andSelectedName:(NSString*)selected andTitle:(NSString*)title
andIndex:(int)index{ //初始化button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = index+; CGFloat buttonWidth = tabBarView.width/;
CGFloat buttonHeight = tabBarView.height;
//设置button位置及大小,注意:要留出中间特殊button的位置
if (index<) {
button.frame = (CGRect){+*index,,buttonWidth,buttonHeight};
}else
button.frame = (CGRect){+*(index+),,buttonWidth,buttonHeight}; [button setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:selected] forState:UIControlStateSelected]; //设置buttonLabel(tabBar的文字)
[button.titleLabel setFont:[UIFont systemFontOfSize:]];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithWhite:0.5 alpha:] forState:UIControlStateNormal];
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected]; [button setTitleEdgeInsets:(UIEdgeInsets){, -, , }]; //button动作
[button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
button.imageView.contentMode =UIViewContentModeCenter;
[tabBarView addSubview:button]; UIButton *bt = tabBarView.subviews[];
[self changeViewController:bt]; } - (void)creatCenterButton:(NSString*)centerImage{ //初始化中间特殊button
UIButton *centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
//位置及大小
centerButton.frame = (CGRect){,,,tabBarView.height-};
//图片
[centerButton setImage:[UIImage imageNamed:centerImage] forState:UIControlStateNormal];
//动作
[centerButton addTarget:self action:@selector(addViewController) forControlEvents:UIControlEventTouchUpInside];
//加到tabBarView上
[tabBarView addSubview:centerButton]; } //换页和button的动作关联上
- (void)changeViewController:(UIButton*)button { if (self.selectedIndex == button.tag-) {
return;
} self.selectedIndex = button.tag-; button.selected = YES; if (lastSelectedButton != button) {
lastSelectedButton.selected = NO;
}
lastSelectedButton = button; self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:self.selectedIndex]; } //中间button点击动作
- (void)addViewController{
AddViewController *AddVC = [[AddViewController alloc] init];
[self presentViewController:AddVC animated:YES completion:nil]; }

ios基础篇(九)——自定义UITabBar的更多相关文章

  1. ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button

    这篇文章我们主要来拿官方的控件来研究一下,我们来仿照官方的控件,自己来实现它提供的控件: 首先来看看基本的图片与文字的绘制,很简单. 一.imageView 所有的视图都是继承自UIView,所以我们 ...

  2. ios基础篇(二十九)—— 多线程(Thread、Cocoa operations和GCD)

    一.进程与线程 1.进程 进程是指在系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内: 如果我们把CPU比作一个工厂,那么进程就好比工厂的车间,一个工厂有 ...

  3. ioS基础篇(十九)——UIResponder简析

    UIResponder类定义了对象相应和控制事件的接口,他是UIApplication.UIView的超类,这类的实例通常被称为应答对象. 一.Responder对象 在iOS系统中,能够响应并处理事 ...

  4. ios基础篇(十六)——UIWebView的基本使用

    UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档等.UIWebView是一个混合体,具体的功能控件内置的,实现一些基本的功能.UIWebView可以查看Html网页,pdf文件,do ...

  5. ios基础篇(三)——UIButton的详细介绍

    按钮UIButton是ios开发中最常见的控件之一,下面来介绍UIButton的详细内容: 一.UIButton的定义 UIButton *button=[[UIButton buttonWithTy ...

  6. ios基础篇(十四)——UITableView(二)属性及基本用法

    上一篇说了UITableView的重用机制,让我们对UITableView有了简单了解,下面说说UITableView的属性及常见方法. 一.属性 1.frame:设置控件的尺寸和大小 2.backg ...

  7. iOS中 UITabBarController中自定义UITabBar

    1.创建多个视图控制器,放如UITabBarController中 AViewController *aa = [[AViewController alloc] init]; UINavigation ...

  8. ios基础篇(二十七)—— Json解析

    一.什么是Json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使 ...

  9. ios基础篇(二十六)—— UITableViewCell的分组索引与标记

    一.表视图的索引目录 首先要创建一个TableView,之前有说过,这里就不详细说了(参考前面第十四篇). 直接贴代码吧, #import "ViewController.h" @ ...

随机推荐

  1. ABAP锁、数据库锁

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. 【CC评网】2013.第44周 把握每天的第一个小时

    [CC评网]2013.第44周 把握每天的第一个小时 更简单的格式 终于投入到markdown的怀抱.让博客的写作回归到内容本身,同时也能保证阅读的良好体验:如果有心情,写个js,提取h3 h2标题组 ...

  3. hdu 2196 Computer 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  4. HDU 5430 Reflect(欧拉函数)

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5430 从镜面材质的圆上一点发出一道光线反射NNN次后首次回到起点. 问本质不同的发射的方案数. 输入描述 ...

  5. git学习笔记02-创建一个仓库提交一个文件-原来就是这么简单

    打开安装好的git bash,设置你的git信息  (这个随便写就行) 初始化一个Git仓库,分三步.(创建文件夹也可以手动创建,也可以命令行创建) 第一步,进到一个目录  cd e: 第二步,创建一 ...

  6. SQL 语句转换格式函数Cast、Convert

    CAST和CONVERT都经常被使用.特别提取出来作为一篇文章,方便查找. CAST.CONVERT都可以执行数据类型转换.在大部分情况下,两者执行同样的功能,不同的是CONVERT还提供一些特别的日 ...

  7. 新浪博客地址 http://blog.sina.com.cn/u/2145079955

    原来 新浪博客地址 http://blog.sina.com.cn/u/2145079955

  8. iOS - OC 术语表

    1.术语表

  9. poj1819Disks

    链接 题意:从左到右按顺序给你n个圆的半径,把左右两边想象成两堵墙的话,就是左右两边向里挤压,问哪些圆是对最后的宽度不影响. 刚开始理解错了,..以为怎么放圆使宽度最小.. 这样就可以尽量使每个圆向左 ...

  10. Mono for Android布局控件属性小结

    1. layout_weight 用于给一个线性布局中的诸多视图的重要度赋值. 所有的视图都有一个layout_weight值,默认为零,意思是需要显示 多大的视图就占据多大的屏幕空 间.若赋一个高于 ...