上一篇讲到了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. hdu 1075 (map)

    http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: 10000/5000 MS ...

  2. HDU 1728 逃离迷宫

    [题目描述 - Problem Description] 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,glo ...

  3. Base64加密解密原理以及代码实现(VC++)

    Base64加密解密原理以及代码实现 转自:http://blog.csdn.net/jacky_dai/article/details/4698461 1. Base64使用A--Z,a--z,0- ...

  4. 关于deferred

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. iOS - MVP 架构模式

    1.MVP 从字面意思来理解,MVP 即 Modal View Presenter(模型 视图 协调器),MVP 实现了 Cocoa 的 MVC 的愿景.MVP 的协调器 Presenter 并没有对 ...

  6. JMeter入门合集

    JMeter从入门到精通 http://blog.csdn.net/lihengxin/article/details/4325918 jmeter入门教程- Jmeter教程及技巧汇总 http:/ ...

  7. show slave status

    Slave_IO_State: Waiting for master to send event                   Master_Host: 10.1.1.1             ...

  8. Android开发设计模式之——单例模式关于线程不安全问题处理

    单例模式是设计模式中最常见也最简单的一种设计模式,保证了在程序中只有一个实例存在并且能全局的访问到.比如在Android实际APP 开发中用到的 账号信息对象管理, 数据库对象(SQLiteOpenH ...

  9. Android监听Button和ImageButton控件的点击事件

    一.onClick事件 Button和ImageButton都有一个onClick事件,通过自身的.setOnClickListener(OnClickListener)方法添加点击事件 所有的控件都 ...

  10. JavaScript数学揭密之函数与勾股定理

    一.函数 function show(n){ return n*2; } alert( show(2) ); alert( show(3) ); alert( show(4) ); 二.勾股定理 1. ...