iOS(三)实现App底部TabBar的切换:二
来源:互联网 发布:米那斯提力斯 知乎 编辑:程序博客网 时间:2024/05/22 14:08
上一篇讲述了iOS自带的TabBar,但在我所见到的很多App源码中大多用了自己写的TabBar,惯例先上图:
这只是一个最简单的TabBar,但重在原理,虽然是我懒。。。。
添加自定义的TabBar文件
ItemView.h
#import <UIKit/UIKit.h>
//用来定义tabbar按钮大小位置,图片,被选中时的颜色
@interface ItemView :UIControl
{
UIImageView *Itemimageview;
UILabel *Itemlabel;
}
-(void)setItemImage:(UIImage *)image forState:(UIControlState)state;//某种状态下的图片
-(void)setItemTitle:(NSString *)title;//tabbar按钮下的文字
-(void)setItemSelected:(BOOL)isSelected;//当定义的按钮被点击时,告诉被选中了
@end
ItemView.m
#import "ItemView.h"
@implementation ItemView
-(instancetype)initWithFrame:(CGRect)frame{
self=[superinitWithFrame:frame];
if (self) {
CGFloat itemHeight=CGRectGetHeight(frame);
CGFloat itemWidth =CGRectGetWidth(frame);
Itemimageview=[[UIImageViewalloc]initWithFrame:CGRectMake((itemWidth-22)/2,10.f,22.f,20.f)];
Itemimageview.contentMode=UIViewContentModeScaleAspectFit;
[self addSubview:Itemimageview];
//定义iamgeView的位值大小,以及图片contentMode属性
Itemlabel=[[UILabelalloc]initWithFrame:CGRectMake(0.f, itemHeight-20.f,
itemWidth, 20.f)];
Itemlabel.textAlignment=NSTextAlignmentCenter;//字体居中
Itemlabel.backgroundColor=[UIColorclearColor];
Itemlabel.font=[UIFontboldSystemFontOfSize:9.f];
Itemlabel.highlightedTextColor=[UIColorblackColor];
Itemlabel.textColor=[UIColorcolorWithRed:254.f/255.f
green:87.f/255.f
blue:178.f/255.f
alpha:1];
[self addSubview:Itemlabel];
}
returnself;
}
-(void)setItemImage:(UIImage *)image forState:(UIControlState)state{
if (state==UIControlStateNormal) {
Itemimageview.image=image;
} elseif(state==UIControlStateSelected){
Itemimageview.highlightedImage=image;
}
}
-(void)setItemTitle:(NSString *)title{
Itemlabel.text=title;
}
-(void)setItemSelected:(BOOL)isSelected{
Itemimageview.highlighted=isSelected;
Itemlabel.highlighted=isSelected;
}
@end
接着修改
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController :UITabBarController
@end
ViewController.m
#import "ViewController.h"
#import "PrivateViewController.h"
#import "ClassViewController.h"
#import "HomeViewController.h"
#import "ItemView.h"
@interfaceViewController ()
{
ItemView *select_TabItem;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfcreateViewControllers];//创建多个视图
[selfcreateTabBarItem];//创建TabBar
// Do any additional setup after loading the view, typically from a nib.
}
-(void)createViewControllers{
ClassViewController *ClassVC=[[ClassViewControlleralloc]init];
PrivateViewController *PrivateVC=[[PrivateViewControlleralloc]init];
HomeViewController *HomeVC=[[HomeViewControlleralloc]init];
NSArray *VCarray=@[HomeVC,ClassVC,PrivateVC];
NSMutableArray *UINavi=[[NSMutableArrayalloc]initWithCapacity:3];
for (int i=0; i<3; i++) {
UINavigationController *navi=[[UINavigationControlleralloc]
initWithRootViewController:VCarray[i]];
[UINavi addObject:navi];
}
self.viewControllers=UINavi;
}
- (void)TabBarclick:(ItemView *)sender {
if (select_TabItem != sender) {
[select_TabItemsetItemSelected:NO];//之前的TabBar变为普通状态
[sender setItemSelected:YES];//被点击的TabBar变为选中状态
select_TabItem = sender;
self.selectedIndex = sender.tag;
//TabBarController的selectedIndex就是用来确定进入到哪个视图,之前总共赋予了三个VC,所以selectedIndex就有0,1,2三个值
}
}
- (void)createTabBarItem{
for(UIView *TabBarIteminself.tabBar.subviews){
[TabBarItem removeFromSuperview];
}//移除原有的TabBarItem
CGFloat itemWidth=self.view.frame.size.width/self.viewControllers.count;
NSArray *titlearray=@[@"首页",@"分类",@"个人"];
NSArray *imagearray=@[@"home",@"classify",@"person"];
NSArray *imagearray_select=@[@"home_s",@"classify_s",@"person_s"];
for (int i=0; i<3; i++) {
ItemView *TabItem=[[ItemViewalloc]initWithFrame:CGRectMake(itemWidth*i,0, itemWidth, self.tabBar.frame.size.height)];
[TabItem addTarget:selfaction:@selector(TabBarclick:)forControlEvents:UIControlEventTouchUpInside];
//给TabBar添加点击的操作
TabItem.tag=i;
[TabItem setItemImage:[UIImageimageNamed:imagearray[i]]forState:UIControlStateNormal];
//给TabBar添加普通情况的图片
[TabItem setItemImage:[UIImageimageNamed:imagearray_select[i]]forState:UIControlStateSelected];
//给TabBar添加被选中时的图片
[TabItem setItemTitle:titlearray[i]];
//给每个TabBar添加文字
[self.tabBaraddSubview:TabItem];
if (i==0) {
[TabItem setItemSelected:YES];
select_TabItem=TabItem;
}
//默认情况下,第一个被选中,所以设置成选中状态
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
下一篇 自己写的动漫App http://blog.csdn.net/u012723810/article/details/50481050
- iOS(三)实现App底部TabBar的切换:二
- iOS(三)实现App底部TabBar的切换:一
- 猫猫学iOS(四十三)之网易彩票底部自定义TabBar实现切换
- AJ学IOS(43)之网易彩票底部自定义TabBar实现切换
- IOS_实现TabBar切换时底部切换效果
- IOS点击底部tabbar的事件
- android tabbar切换的实现
- ImageView实现APP底部按钮切换页面
- 隐藏底部的tabbar
- iOS UITabBarController底部tabbar的item间距设置
- iOS隐藏导航条、tabbar 1px的底部横线
- ios 底部的4个tabBar回顾学习
- 献给初学iOS的小盆友们——微博app项目开发之二自定义tabBar
- 一个带动画的页面底部的TabBar的实现
- iOS 隐藏tabbar会导致底部tabbar的位置的空间不能被点击响应
- iOS跳转界面时隐藏tabBar的方法(隐藏tabBar底部变黑)
- IOS国际化(三)app内部语言的切换
- Android自定义控件—-RadioGroup实现APP首页底部Tab的切换
- APP功能测试点
- 差分约束系统
- px、dp和sp,这些单位有什么区别?
- 简单邮件服务(Postfix+dovecot)
- hdu 1050 acmsteps moving tables
- iOS(三)实现App底部TabBar的切换:二
- linux配置matconvnet(GPU support)
- AvalonDock 2.0+Caliburn.Micro+MahApps.Metro实现Metro风格插件式系统(菜单篇)
- SubString(i,j)体悟
- HTTP/1.1 新建会话失败
- jQuery选择器汇总
- Fibonacci数列、判断回文字符串
- MATLAB的Roberts算子与Sobel算子
- 1000以内的完数