StackoverFlow上看到的,通过继承UITabBarController创建自定义TabBarController。在原有TabBar的基础上添加一个背景层,在其基础上增加三个自定义按钮,通过设置按钮的背景图片及大小即可简单实现TabBar的自定义。

// CustomTabBarController.h

#import <UIKit/UIKit.h>

@interface CustomTabBarController : UITabBarController {

UIButton *settingsButton;

UIButton *infoButton;

UIButton *aboutUsButton;

}

@property (nonatomic, retain) UIButton *settingsButton;

@property (nonatomic, retain) UIButton *infoButton;

@property (nonatomic, retain) UIButton *aboutUsButton;

-(void) addCustomElements;

-(void) selectTab:(int)tabID;

@end

// CustomTabBarController.m

#import “CustomTabBarController.h”

@implementation CustomTabBarController

@synthesize settingsButton, infoButton, aboutUsButton;

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];

}

-(void)viewDidLoad

{

[super viewDidLoad];

[self addCustomElements];

}

-(void)addCustomElements

{

// Background

UIImageView* bgView = [[[UIImageView alloc] initWithImage:[UIImageimageNamed:@”tabBarBackground.png”]] autorelease];

bgView.frame = CGRectMake(0, 420, 320, 60);

[self.view addSubview:bgView];

// Initialise our two images

UIImage *btnImage = [UIImage imageNamed:@”settings.png”];

UIImage *btnImageSelected = [UIImage imageNamed:@”settingsSelected.png”];

self.settingsButton = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button

settingsButton.frame = CGRectMake(10, 426, 100, 54); // Set the frame (size and position) of the button)

[settingsButton setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button

[settingsButton setBackgroundImage:btnImageSelectedforState:UIControlStateHighlighted]; // Set the image for the selected state of the button

[settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];// Set the image for the selected state of the button

[settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateDisabled];

[settingsButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];

[settingsButton setTag:101]; // Assign the button a “tag” so when our “click” event is called we know which button was pressed.

[settingsButton setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially

// Now we repeat the process for the other buttons

btnImage = [UIImage imageNamed:@”info.png”];

btnImageSelected = [UIImage imageNamed:@”infoSelected.png”];

self.infoButton = [UIButton buttonWithType:UIButtonTypeCustom];

infoButton.frame = CGRectMake(110, 426, 100, 54);

[infoButton setBackgroundImage:btnImage forState:UIControlStateNormal];

[infoButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];

[infoButton setBackgroundImage:btnImageSelected forState:UIControlStateHighlighted];

[infoButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];

[infoButton setTag:102];

btnImage = [UIImage imageNamed:@”aboutUs.png”];

btnImageSelected = [UIImage imageNamed:@”aboutUsSelected.png”];

self.aboutUsButton = [UIButton buttonWithType:UIButtonTypeCustom];

aboutUsButton.frame = CGRectMake(210, 426, 100, 54);

[aboutUsButton setBackgroundImage:btnImage forState:UIControlStateNormal];

[aboutUsButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];

[aboutUsButton setBackgroundImage:btnImageSelectedforState:UIControlStateHighlighted];

[aboutUsButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];

[aboutUsButton setTag:103];

// Add my new buttons to the view

[self.view addSubview:settingsButton];

[self.view addSubview:infoButton];

[self.view addSubview:aboutUsButton];

// Setup event handlers so that the buttonClicked method will respond to the touch up inside event.

[settingsButton addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];

[infoButton addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];

[aboutUsButton addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];

}

- (void)buttonClicked:(id)sender

{

int tagNum = [sender tag];

[self selectTab:tagNum];

}

- (void)selectTab:(int)tabID

{

switch(tabID)

{

case 101:

[settingsButton setSelected:true];

[infoButton setSelected:false];

[aboutUsButton setSelected:false];

break;

case 102:

[settingsButton setSelected:false];

[infoButton setSelected:true];

[aboutUsButton setSelected:false];

break;

case 103:

[settingsButton setSelected:false];

[infoButton setSelected:false];

[aboutUsButton setSelected:true];

break;

}

self.selectedIndex = tabID;

}

- (void)dealloc {

[settingsButton release];

[infoButton release];

[aboutUsButton release];

[super dealloc];

}

@end

转载自: http://blog.163.com/l1_jun/blog/static/143863882012101545146269/

 

简单实现TabBar的自定义的更多相关文章

  1. 简单的例子了解自定义ViewGroup(一)

    在Android中,控件可以分为ViewGroup控件与View控件.自定义View控件,我之前的文章已经说过.这次我们主要说一下自定义ViewGroup控件.ViewGroup是作为父控件可以包含多 ...

  2. KVC替换系统的tabbar为自定义tabbar---秀清

    CustomTabbar *tabbar = [[CustomTabbar alloc]init]; //KVC,更换系统的tabbar为自定义tabbar tabbar.tabbarDelegate ...

  3. 简单实现Tabbar的隐藏显示动画 By H罗

    简单实现Tabbar的隐藏显示动画 Hide Tabbar Controller with Animation - (void)setTabBarVisible:(BOOL)visible anima ...

  4. FusionCharts简单教程(三)-----如何自定义图表上的工具提示

    最近有蛮多人总是问我这个FusionCharts制表的问题,帮助他们解决之后,在昨晚发现以前整理的笔记中有这个简单教程,而且以前也发表了几篇这个博文,所以就将其全部上传上来供别人参考.如有不正确之处望 ...

  5. [安卓] 18、一个简单的例子做自定义动画按钮和自定义Actionbar

    在做安卓UI的时候有时候需自定义具有动画效果的按钮或需要自定义一下actionbar~ 本节用一个简单的demo讲如何自定义具有动画效果的按钮,以及个性化的actionbar 下面是效果: 其中: △ ...

  6. 简单天气应用开发——自定义TableView

    顺利解析JSON数据后,天气数据已经可以随意提取了,现在要做的就是建立一个简单的UI. 实况信息较为简单,几个Lable就可以解决.主要是七天天气预报有点麻烦,那是一个由七个字典构成的数组,需要提取出 ...

  7. iOS彩票项目--第一天,自定义TabBar控制器和自定义TabBar,自定义导航控制器

    一.环境配置,和项目层次搭建 二.自定义TabBar 项目中TabBar中的导航按钮美工给的图片太大,图片中包含了图片和文字.最主要的是TabBar上面的按钮图片尺寸是有规定的,当高度大于44的时候, ...

  8. FusionCharts简单教程(六)-----如何自定义图表上的工具提示

          所谓图表上的工具提示就是当鼠标放在某个特定的数据块上时所显示的提示信息.如下: 禁用显示工具提示       在默认情况下工具提示功能是显示的,但是有时候我们并不是很想需要这个功能提示功能 ...

  9. structs2标签简单实用,及自定义转换器示例代码

    一.在structs.xml中配置 <structs> <package name="tagp" namespace="/test" exte ...

随机推荐

  1. react面试宝典

    调用 setState 之后发生了什么? 在代码中调用setState函数之后,React 会将传入的参数对象与组件当前的状态合并,然后触发所谓的调和过程(Reconciliation).经过调和过程 ...

  2. 用css3技术给网站加分

    自己写了几个小DEMO,请打开http://codepen.io/shenggen/

  3. ubuntu搭建ftp

    腾讯云服务器linux Ubuntu操作系统安装ftp服务器vsftpd Ubuntu Server 16.04.1 LTS 64位 登录服务器  xshell 登录服务器 用户名如果没自定义默认:u ...

  4. Struts2页面遍历

    <s:iterator />可以遍历 数据栈里面的任何数组,集合等等 在使用这个标签的时候有三个属性值得我们关注      1. value属性:可选的属性,value属性是指一个被迭代的 ...

  5. OC浅析一

    Objective-C是一门简单的语言,95%是C.只是在语言层面上加了些关键字和语法.真正让Objective-C如此强大的是它的运行时.它很小但却很强大.它的核心是消息分发. 在Objective ...

  6. RobotFramework教程使用笔记——RobotFramework的安装配置

    (一)  Python的安装和pip配置 首先,从Python的官方网站 www.python.org http://www.python.org/ftp/python 然后,运行下载的MSI安装包, ...

  7. noip2005篝火晚会

    这是一道不算太难的题,但愚蠢的我并没有想到. 首先,判断无解的情况:他想相邻的不想与他相邻. 然后,构造出合法的数列,因为第一位左边有两种选择,且构造出的环不等价,所以要做两次. (这一点我并没有想清 ...

  8. Vue表格数据增删改查及搜索

    <div id="app"> <div class="item"> <span class="name"> ...

  9. [Balkan 2007] Mokia

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1176 [算法] CDQ分治 + 树状数组即可 时间复杂度 : O(Nlog^2N) ...

  10. Ubuntu16.04 安装cuda9.0 cudnn 7.0.5

    参考网址:https://blog.csdn.net/zhuangwu116/article/details/81063234 (1)下载安装文件: 下载cuda9.0 runfile 文件 下载地址 ...