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" @ ...
随机推荐
- NoSQL聚合数据模型
NoSQL聚合数据模型 特点 聚合数据模型的特点就是把经常访问的数据放在一起(聚合在一块): 这样带来的好处很明显,对于某个查询请求,能够在与数据库一次交互中将所有数据都取出来: 当然,以这种方式存储 ...
- python_way day21 Django文件上传Form方式提交,原生Ajax提交字符处啊,Django文件上传之原生Ajax方式、jQuery Ajax方式、iframe方式,Django验证码,抽屉示例,
python_way day21 1.Django文件上传至Form方式 2.原生Ajax文件上传提交表单 使用原生Ajax好处:不依赖jquery,在发送一个很小的文件或者字符串的时候就可以用原生A ...
- 一张png图片 上面有多个图标,如何用CSS准确的知道其中某个图片的坐标
一张png图片 上面有多个图标,如何用CSS准确的知道其中某个图片的坐标 ,如下图 可以使用 background background:url(images/xx.png) 40px 10px n ...
- iOS - OC NSSet 集合
前言 NSSet:集合 @interface NSSet<__covariant ObjectType> : NSObject <NSCopying, NSMutableCopyin ...
- html一般标签、常用标签、表格
body的属性: bgcolor 页面背景色 text 文字颜色 topmargin 上边距 leftmargi ...
- Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- Android中的启动模式(下)
在这篇文章中,我会继续跟大家分享有关于Android中启动模式的相关知识.当然,如果对这个启动模式还不完全了解或者没有听过的话,可以先看看我之前写的有关于这个知识点的入门篇Android的启动模式(上 ...
- java中compareTo和compare方法之比较
这两个方法经常搞混淆,现对其进行总结以加深记忆. compareTo(Object o)方法是java.lang.Comparable接口中的方法,当需要对某个类的对象进行排序时,该类需要实现Comp ...
- 牛客网 --java问答题
http://www.nowcoder.com/ 主要是自己什么都不怎么会.在这里可以学习很多的! 第一天看题自己回答,第二天看牛客网的答案! 1 什么是Java虚拟机?为什么Java被称作是“平台无 ...
- Oracle Names - Oracle_SID /db_name instance_name service_names / service_name / sid / sid_name
很多人还是困惑,下面再次尝试从几个不同角度区分一下: Oracle_SID / db_name, instance_name, service_names / service_name, sid / ...