iOS开发基础 - UITabBarController
2016-03-25 10:40
260 查看
UITabBarController
// mineVC.tabBarItem.badgeValue = @”2”;
// tabBarCon.tabBar.barStyle = UIBarStyleBlack;
/*
UIBarStyleDefault
UIBarStyleBlack
UIBarStyleBlackOpaque = 1, // Deprecated. Use UIBarStyleBlack
UIBarStyleBlackTranslucent = 2,
*/
//tabBar背景颜色
// tabBarCon.tabBar.barTintColor = [UIColor cyanColor];
//tabBar背景图片
tabBarCon.tabBar.backgroundImage = [UIImage imageNamed:@”home_tabbar”];
//导航条的高度 64
//标签条的高度 49
// tabBarCon.selectedViewController = findVC;
2.隐藏系统的标签栏 然后重写
@interface MyTabBarController ()
@end
@implementation MyTabBarController
(void)viewDidLoad {
[super viewDidLoad];
//隐藏系统标签条
self.tabBar.hidden = YES;
[self create];
[self createTabBar];
}
-(void)create{
RootViewController * rootVC = [[RootViewController alloc] init];
rootVC.title = @”首页”;
SecondViewController * secondVC = [[SecondViewController alloc] init];
secondVC.title = @”第二页”;
ThirdViewController * thirdVC = [[ThirdViewController alloc] init];
thirdVC.title = @”第三页”;
FouthViewController * fouthVC = [[FouthViewController alloc] init];
fouthVC.title = @”第四页”;
LastViewController * lastVC = [[LastViewController alloc] init];
lastVC.title = @”第五页”;
}
-(void)createTabBar{
}
-(void)changeState:(UIButton *)btn{
// for (int i = 100; i< 105; i++){
// UIButton * btn1 = (id)[self.view viewWithTag:i];
// btn1.selected = NO;
// }
// static int value = 100;
// UIButton * btn1 = (id)[self.view viewWithTag:value];
// btn1.selected = NO;
// btn.selected = YES;
// value = btn.tag;
btn.selected = YES;
}
协议使用
//第一个参数tabBarController 第二参数:是当前选中的界面
点击某个页面会触发这个方法 并且将点击的页面传入 是否可以被选择
- (BOOL)tabBarController:(UITabBarController )tabBarController shouldSelectViewController:(UIViewController )viewController{
}
(void)tabBarController:(UITabBarController )tabBarController didSelectViewController:(UIViewController )viewController{
//点击每个界面会调用这个方法
}
//标签栏控制器有一个标签条 是属于标签栏控制器的UITabBarController //标签条上面的按钮tabBarItem 是属于每个界面的 HomeViewController *homeVC = [[HomeViewController alloc]init]; //tabBarItem标签项 homeVC.tabBarItem.image = [UIImage imageNamed:@"home_tabbar_icon_home"]; homeVC.title = @"首页"; SortViewController *sortVC = [[SortViewController alloc]init]; //tabBarItem未选中的图片 sortVC.tabBarItem.image = [UIImage imageNamed:@"home_tabbar_icon_channel"]; //tabBarItem下面的标题 sortVC.title = @"分类"; //tabBarItem选中的图片 sortVC.tabBarItem.selectedImage = [UIImage imageNamed:@"home_tabbar_icon_channel_sel"]; FindViewController *findVC = [[FindViewController alloc]init]; findVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"发现" image:[UIImage imageNamed:@"home_tabbar_icon_find"] selectedImage:[UIImage imageNamed:@"home_tabbar_icon_find_sel"]]; MineViewController *mineVC = [[MineViewController alloc]init]; mineVC.title = @"我的"; mineVC.tabBarItem.image = [UIImage imageNamed:@"home_tabbar_icon_user"]; mineVC.tabBarItem.selectedImage = [UIImage imageNamed:@"home_tabbar_icon_user_sel"]; //设置提示红点
// mineVC.tabBarItem.badgeValue = @”2”;
//UITabBarController 标签栏控制器 它也是一个容器 用来管理平级界面之间的切换 继承自UIViewController //UINavigationController 导航控制器 也是一个容器 用来管理具有明确层次关系的界面之间的切换 继承自UIViewController UITabBarController *tabBarCon = [[UITabBarController alloc]init]; //把平级界面放到数组里面 viewControllers是UITabBarController的属性 tabBarCon.viewControllers = @[homeVC,sortVC,findVC,mineVC]; //数组中元素的顺序 就是显示的顺序 //tabBar 标签条 属于UITabBarController //barStyle标签条的风格
// tabBarCon.tabBar.barStyle = UIBarStyleBlack;
/*
UIBarStyleDefault
UIBarStyleBlack
UIBarStyleBlackOpaque = 1, // Deprecated. Use UIBarStyleBlack
UIBarStyleBlackTranslucent = 2,
*/
//tabBar背景颜色
// tabBarCon.tabBar.barTintColor = [UIColor cyanColor];
//tabBar背景图片
tabBarCon.tabBar.backgroundImage = [UIImage imageNamed:@”home_tabbar”];
//导航条的高度 64
//标签条的高度 49
//默认选中的界面/默认显示的界面 tabBarCon.selectedIndex = 1;
// tabBarCon.selectedViewController = findVC;
//把标签栏控制器作为window的根试图 self.window.rootViewController = tabBarCon; [self.window makeKeyAndVisible]; return YES;
UITabBarController的自定义使用
1.建立新类型 继承自UITabBarController2.隐藏系统的标签栏 然后重写
@interface MyTabBarController ()
@end
@implementation MyTabBarController
(void)viewDidLoad {
[super viewDidLoad];
//隐藏系统标签条
self.tabBar.hidden = YES;
[self create];
[self createTabBar];
}
-(void)create{
RootViewController * rootVC = [[RootViewController alloc] init];
rootVC.title = @”首页”;
SecondViewController * secondVC = [[SecondViewController alloc] init];
secondVC.title = @”第二页”;
ThirdViewController * thirdVC = [[ThirdViewController alloc] init];
thirdVC.title = @”第三页”;
FouthViewController * fouthVC = [[FouthViewController alloc] init];
fouthVC.title = @”第四页”;
LastViewController * lastVC = [[LastViewController alloc] init];
lastVC.title = @”第五页”;
self.viewControllers = @[rootVC,secondVC,thirdVC,fouthVC,lastVC];
}
-(void)createTabBar{
CGFloat width = [UIScreen mainScreen].bounds.size.width; CGFloat heigth = [UIScreen mainScreen].bounds.size.height; //创建UIView作为标签条 UIView * tabView = [[UIView alloc] initWithFrame:CGRectMake(0, heigth - 44, width, 44)]; tabView.backgroundColor = [UIColor orangeColor]; tabView.tag = 200; //添加背景图 UIImageView * backImage = [[UIImageView alloc] initWithFrame:tabView.frame]; backImage.image = [UIImage imageNamed:@"naviback_n"]; [tabView addSubview:backImage]; NSArray * titleArr = @[@"I",@"M",@"P",@"Q",@"V"]; for (int i = 0; i < 5; i++) { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(i * width/5.0, 0, width/5, 44); //未选中状态 NSString * norName =[NSString stringWithFormat:@"btn%@-n",titleArr[i]]; [btn setBackgroundImage:[UIImage imageNamed:norName] forState:UIControlStateNormal]; //选中状态图片 NSString * selName = [NSString stringWithFormat:@"btn%@-h",titleArr[i]]; [btn setBackgroundImage:[UIImage imageNamed:selName] forState:UIControlStateSelected]; btn.tag= 100+i; if (btn.tag == 100) { btn.selected = YES; } [btn addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventTouchUpInside]; [tabView addSubview:btn]; } //添加下划线 UIImageView * lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 43, width/5, 2)]; lineImage.backgroundColor = [UIColor whiteColor]; lineImage.tag = 99; [tabView addSubview:lineImage]; [self.view addSubview:tabView];
}
-(void)changeState:(UIButton *)btn{
// for (int i = 100; i< 105; i++){
// UIButton * btn1 = (id)[self.view viewWithTag:i];
// btn1.selected = NO;
// }
// static int value = 100;
// UIButton * btn1 = (id)[self.view viewWithTag:value];
// btn1.selected = NO;
// btn.selected = YES;
// value = btn.tag;
btn.selected = YES;
//tabBar.subviews所有的子视图 UIView * views = [self.view viewWithTag:200]; for (UIView * view in views.subviews) { if ([view isKindOfClass:[UIButton class]]) { //强转 只会改变变量类型不能改变对象类型 UIButton * subBtn = (UIButton *)view; if (subBtn != btn) { subBtn.selected = NO; } } } //切换界面 self.selectedIndex = btn.tag-100; //下划线移动 UIImageView * line = (id)[views viewWithTag:99]; [UIView animateWithDuration:0.5 animations:^{ line.center = CGPointMake(btn.center.x, 43); }];
}
协议使用
//第一个参数tabBarController 第二参数:是当前选中的界面
点击某个页面会触发这个方法 并且将点击的页面传入 是否可以被选择
- (BOOL)tabBarController:(UITabBarController )tabBarController shouldSelectViewController:(UIViewController )viewController{
NSUInteger num = [tabBarController.viewControllers indexOfObject:viewController]; if (num == 1) { return NO; } return YES;
}
(void)tabBarController:(UITabBarController )tabBarController didSelectViewController:(UIViewController )viewController{
//点击每个界面会调用这个方法
}
相关文章推荐
- iOS开发基础-UIPageControl
- iOS开发基础- UIScrollView
- iOS开发基础-UIView的使用
- UIMenuController 实现长按显示自定义菜单功能
- iOS开发基础-UITextView&UITextField
- iOS开发基础-UIImageView&&UIImage的使用
- iOS开发基础-UIButton的使用
- iOS开发基础-UILabel的使用
- ui设计一
- UIViewController的生命周期及界面切换
- BZOJ 2037: [Sdoi2008]Sue的小球
- Building the System 编译安卓系统
- Scala Error: error while loading Suite, Scala signature Suite has wrong version expected: 5.0 found: 4.1 in Suite.class
- Android之UI简介
- iOS UISearchController TableView 实现简单搜索功能
- ligerUi 格式化日期
- 【iOS】iOS之Button segue弹出popOver消除(dismiss)问题
- PHP中include和require的区别详解
- 《iOS Human Interface Guidelines》——Netword Activity Indicator
- QueryPerformanceCounter