iOS 自定义UITabBarController的tabBar
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
/**
* 素材图片的链接: http://pan.baidu.com/s/1geahYRT 密码: axmh
* 注意图片的尺寸,否则会变形
*/
#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h" @interface AppDelegate ()<UITabBarControllerDelegate>
{
float imgWidth;//tabBarController的宽度
UITabBarController *_tabBarController;
NSMutableArray *imageViewArr;
}
@end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; //初始化数组
imageViewArr = [[NSMutableArray alloc] init];
//初始化控制器
FirstViewController *firstVC = [[FirstViewController alloc] init];
SecondViewController *secondVC = [[SecondViewController alloc] init];
ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
FourthViewController *fourthVC = [[FourthViewController alloc] init];
NSArray *array = @[firstVC,secondVC,thirdVC,fourthVC];
//初始化UITabBarController
_tabBarController = [[UITabBarController alloc] init];
//背景图片
[_tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"1.png"]];
//默认选中第一个
_tabBarController.selectedIndex = ;
//设置代理
_tabBarController.delegate = self;
_tabBarController.viewControllers = array;
imgWidth = [UIScreen mainScreen].bounds.size.width/(array.count*1.0);
//初始化tabBarController
[self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"11.png" selectedImageViewTag: selectedImage:@"111.png" tabBarItemTitle:@"首页"];
[self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"12.png" selectedImageViewTag: selectedImage:@"112.png" tabBarItemTitle:@"导航"];
[self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"13.png" selectedImageViewTag: selectedImage:@"113.png" tabBarItemTitle:@"消息"];
[self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"14.png" selectedImageViewTag: selectedImage:@"114.png" tabBarItemTitle:@"更多"];
//设置文字的颜色
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:UITextAttributeTextColor] forState:];
//设置选中时文字的颜色
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor] forState:UIControlStateSelected]; self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
/**
* 初始化tabBarController的tabBar
*
* @param frame 图片的尺寸
* @param imageName 底层图片的名字
* @param tag 图片的标签
* @param selectedImageName 顶层图片的名字
* @param title 标题
*/
- (void)setupView:(CGRect)frame image:(NSString *)imageName selectedImageViewTag:(int)tag selectedImage:(NSString *)selectedImageName tabBarItemTitle:(NSString*)title{
//底层图片
UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
imgView.image = [UIImage imageNamed:imageName];
//顶层图片(不选中时隐藏)
UIImageView *selectedImageView = [[UIImageView alloc] initWithFrame:frame];
selectedImageView.tag = tag;
selectedImageView.image = [UIImage imageNamed:selectedImageName];
if ((tag-) == ) {
selectedImageView.hidden = NO;
}else{
selectedImageView.hidden = YES;
}
[_tabBarController.tabBar addSubview:imgView];
[_tabBarController.tabBar addSubview:selectedImageView];
UITabBar *tabBar = _tabBarController.tabBar;
UITabBarItem *tabBarItem = [tabBar.items objectAtIndex:(tag - )];
//设置标题
tabBarItem.title = title;
//selectedImageView存放到数组
[imageViewArr addObject:selectedImageView];
}
#pragma mark -- UITabBarControllerDelegate的方法 --
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//遍历imageView,选中的就不隐藏,其它的隐藏
for (UIImageView *imageView in imageViewArr) {
if (imageView.tag == +tabBarController.selectedIndex) {
imageView.hidden = NO;
}else{
imageView.hidden = YES;
}
}
} @end
#import <UIKit/UIKit.h> @interface FirstViewController : UIViewController @end
#import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import <UIKit/UIKit.h> @interface SecondViewController : UIViewController @end
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
#import <UIKit/UIKit.h> @interface ThirdViewController : UIViewController @end
#import "ThirdViewController.h" @interface ThirdViewController () @end @implementation ThirdViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
#import <UIKit/UIKit.h> @interface FourthViewController : UIViewController @end
#import "FourthViewController.h"
@interface FourthViewController ()
@end
@implementation FourthViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
@end
iOS 自定义UITabBarController的tabBar的更多相关文章
- iOS 自定义滑动切换TabBar
貌似经常会用到,自己整理收藏起来,方便日后查找备用. 效果如图: 由于制作gif,调整了属性,所以看起来的效果不好.如果用默认配置,生成的gif会很大. 制作gif: 1.使用QuickTimePla ...
- iOS-自定义 UITabBarController
先来回顾一下UITabBarController ( 稍微详细的在在http://blog.csdn.net/yang198907/article/details/49807011) 伴随UITabB ...
- 自定义UITabBarController
用的时候直接拷贝代码即可. 1.在AppDelegate设置跟控制器为:PQTabBarController #import "PQTabBarController.h" @int ...
- 【iOS自定义键盘及键盘切换】详解
[iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...
- iOS自定义的UISwitch按钮
UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...
- 如何实现 iOS 自定义状态栏
给大家介绍如何实现 iOS 自定义状态栏 Sample Code: 01 UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIAp ...
- 自定义UITabbarController控制器
自定义UITabbarController控制器 这是定制UITabbarController的基本原理,没有进行功能性封装. 效果: 源码地址: https://github.com/YouXi ...
- iOS自定义组与组之间的距离以及视图
iOS自定义组与组之间的距离以及视图 //头视图高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...
- iOS 自定义转场动画
代码地址如下:http://www.demodashi.com/demo/12955.html 一.总效果 本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动 ...
随机推荐
- 使用CodeDOM实现代码生成及动态编译
参考资料: http://www.cnblogs.com/lichdr/category/12610.html http://www.cnblogs.com/whitewolf/category/25 ...
- DX11.2 Tiled Resource Pool
Nvidia white paper : https://developer.nvidia.com/content/taking-advantage-directx112-tiled-resource ...
- JS实现HTML静态页传值的方法
JS实现HTML静态页传值的方法 作者:前端开发-武方博 发布:2012-10-29 分类:javascript 阅读:8,735次 此处使用JS方式实现静态页之间值传递,其实很简单,废话不多 ...
- 从cookie的setDomain方法可以得知localhost不是域名
http://www.baidu.com 其中baidu.com是一个域名.那么http://localhost 中的localhost是不是域名呢?我百度过,发现有人说这是域名.于是我在自己的web ...
- SDP协议中的Continuation State
在SDP request和SDP response中,最后一部分为Continuation State,结构如下: 它用于一次response不够把所有的Data传回去的情况.这时候需要将respon ...
- 窗口类型(Widget, Window, Dialog, Desktop, SubWindow等等)
http://doc.qt.io/qt-5/qwidget.html#windowFlags-prop http://doc.qt.io/qt-5/qtwidgets-widgets-windowfl ...
- qt 4.6.2 与visual studio 2005 集成(编译方法,以及中间遇到的问题)
不知不觉在蒂森差不多半个月了,哈哈,时间过得很快,过得很充实,近来研究QT,首先嘛,肯定要学会安装了,这最基础的不会更不用说下面的啦.闲话少说,进正题,基本的安装步骤网上多的是,但参考一个大多数情况是 ...
- ArcGIS API for Silverlight 当DataGrid选中项时,地图聚焦弹出窗口,并可以播放音频文件
原文:ArcGIS API for Silverlight 当DataGrid选中项时,地图聚焦弹出窗口,并可以播放音频文件 先看效果图,然后上代码: <UserControl x:Class= ...
- SQL Server 用SQL语句查找某个表的触发器
select * from sysobjects where xtype='TR' and parent_obj=object_id('表名') 再用sp_helptext ...
- 我的工具箱之MyEclipse9.1
下载地址:http://pan.baidu.com/s/1bbuN1s 这个工具是用来开发Java程序,自带JDK和Tomcat,功能全面周到,使用方便. 市面上MyEclipse版本很多,但都需要破 ...