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. Hibernate表关系映射之一对一映射

    一.数据表的映射关系 在数据库领域中,数据表和数据表之间关系一般可以分为如下几种: 一对一:比如公民和身份证的关系,一个人只有一张身份证,同时每张身份证也仅仅对应一个人! 一对多:比如客户和订单之间的 ...

  2. 九度OJ 1118:数制转换 (进制转换)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3873 解决:1494 题目描述: 求任意两个不同进制非负整数的转换(2进制-16进制),所给整数在long所能表达的范围之内.     不 ...

  3. ZeroMQ Distributed Messaging

    ZeroMQ \zero-em-queue\, \ØMQ\: Ø  Connect your code in any language, on any platform. Ø  Carries mes ...

  4. 【LeetCode】Insertion Sort List

    Sort a linked list using insertion sort. //用到O(N)的额外空间 public class Solution { public ListNode inser ...

  5. 【Effective C++】让自己习惯C++

    条款01:视C++为一个语言联绑 C++的四个语言层次: C:C++是以C为基础的.基本数据类型.语句.预处理器.数组.指针等统统来自C. Oject-Oriented C++:面向对象这一特性包含了 ...

  6. React + fetch API + 百度地图api + 跨域 填坑

    做项目遇到一个百度地图api 的跨域问题.由于使用fetch ,在调用类似 http://api.map.baidu.com/geocoder/v2/callback=renderReverse&am ...

  7. Spring Boot缓存源码分析

    前言 项目里面要增加一个应用缓存,原本想着要怎么怎么来整合ehcache和springboot,做好准备配置这个配置那个,结果只需要做三件事: pom依赖 写好一个ehcache的配置文件 在boot ...

  8. hdu1078 FatMouse and Cheese —— 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...

  9. BZOJ_2216_[Poi2011]Lightning Conductor_决策单调性

    BZOJ_2216_[Poi2011]Lightning Conductor_决策单调性 Description 已知一个长度为n的序列a1,a2,...,an. 对于每个1<=i<=n, ...

  10. su 和sudo 命令

    一. 使用 su 命令临时切换用户身份 1.su 的适用条件和威力 su命令就是切换用户的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要添加用户任务,执行useradd ,beinan用 ...