ios基础篇(九)——自定义UITabBar
上一篇讲到了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
//
// 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的更多相关文章
- ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button
这篇文章我们主要来拿官方的控件来研究一下,我们来仿照官方的控件,自己来实现它提供的控件: 首先来看看基本的图片与文字的绘制,很简单. 一.imageView 所有的视图都是继承自UIView,所以我们 ...
- ios基础篇(二十九)—— 多线程(Thread、Cocoa operations和GCD)
一.进程与线程 1.进程 进程是指在系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内: 如果我们把CPU比作一个工厂,那么进程就好比工厂的车间,一个工厂有 ...
- ioS基础篇(十九)——UIResponder简析
UIResponder类定义了对象相应和控制事件的接口,他是UIApplication.UIView的超类,这类的实例通常被称为应答对象. 一.Responder对象 在iOS系统中,能够响应并处理事 ...
- ios基础篇(十六)——UIWebView的基本使用
UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档等.UIWebView是一个混合体,具体的功能控件内置的,实现一些基本的功能.UIWebView可以查看Html网页,pdf文件,do ...
- ios基础篇(三)——UIButton的详细介绍
按钮UIButton是ios开发中最常见的控件之一,下面来介绍UIButton的详细内容: 一.UIButton的定义 UIButton *button=[[UIButton buttonWithTy ...
- ios基础篇(十四)——UITableView(二)属性及基本用法
上一篇说了UITableView的重用机制,让我们对UITableView有了简单了解,下面说说UITableView的属性及常见方法. 一.属性 1.frame:设置控件的尺寸和大小 2.backg ...
- iOS中 UITabBarController中自定义UITabBar
1.创建多个视图控制器,放如UITabBarController中 AViewController *aa = [[AViewController alloc] init]; UINavigation ...
- ios基础篇(二十七)—— Json解析
一.什么是Json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使 ...
- ios基础篇(二十六)—— UITableViewCell的分组索引与标记
一.表视图的索引目录 首先要创建一个TableView,之前有说过,这里就不详细说了(参考前面第十四篇). 直接贴代码吧, #import "ViewController.h" @ ...
随机推荐
- python_way ,day11 进程
if __name__ == '__main__': for i in range(10): p = Process(target=foo, args=(i,)) # p.daemon = True ...
- mysql获得自增字段下一个值
初次研究: 表: sql: show table status from carsale_db LIKE 'tb_car' 结果: 想办法取得这其中的值.... 在Internet上找到这个资料: M ...
- VirtualBox全屏切换
用VirtualBox的时候,如果设置为全屏,想再切换回来,没有什么菜单,只有通过键盘的快捷键来操作,才可以恢复. 我常常忘掉,所以老是得去找,以后需要记住这几个按键的快捷键. 1.全屏与非全屏切换: ...
- php用curl调用接口方法,get和post两种方式
首先是客户端执行方法ApiModel.php: <?php /** * 模拟post进行url请求 * @param string $url * @param array $post_data ...
- 选择列表控件的使用(PickList)
需要下载picklist.dll类库配合使用 <%@ Register TagPrefix="cc1" Namespace="PickListControl&quo ...
- matplotlib库的常用知识
看看matplotlib是什么? matplotlib是python上的一个2D绘图库,它可以在夸平台上边出很多高质量的图像.综旨就是让简单的事变得更简单,让复杂的事变得可能.我们可以用matplot ...
- Java编程思想学习笔记_3(继承,内部类)
一.继承与清理 如果某个类需要去清理自身的资源,那么必须用心为其创建回收垃圾的方法,而如果此类有导出的子类,那么必须在导出类中覆盖回收的方法,当覆盖被继承类的回收垃圾的方法的时候,需要注意销毁的顺序应 ...
- bzoj3529(莫比乌斯反演+离线+树状数组)
在你以为理解mobus的时候,苦苦想通过化简公式来降低复杂度时,这题又打了我一巴掌. 看来我并没有理解到acmicpc比赛的宗旨啊. 这么多次查询可以考虑离线操作,使用树状数组单点更新. /***** ...
- java按值传递相关理解
Java没有引用传递只有按值传递,没有引用传递只有按值传递,值传递. 1. public class Test { public static void main(String[] args ...
- @requestBody注解的使用
1.@requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,比如说:application/json或者是ap ...