【UI初级------连载七】标签控制器
来源:互联网 发布:caffe loss大小 编辑:程序博客网 时间:2024/06/06 09:09
内容简介:
1、创建标签控制器
2、标签栏(TabBar)的设置
3、创建子控制器
4、标签控制器和导航控制器的结合
一、创建标签控制器(.m文件中)
#import"HomeViewController.h"
#import "MessageViewController.h"
#import "SearchViewController.h"
#import "MessageViewController.h"
#import "SearchViewController.h"
#import"SettingViewController.h"
//创建几个视图控制器
HomeViewController *homeVC = [[HomeViewControlleralloc]init];
homeVC.title= @"home";
//最好不要在这里设置视图控制器的title,这样会破坏它的封装性,要在它自己的.m文件中设置
MessageViewController *messageVC = [[MessageViewControlleralloc]init];
messageVC.title= @"message";
SearchViewController *searchVC = [[SearchViewControlleralloc]init];
searchVC.title= @"search";
SettingViewController *settingVC = [[SettingViewControlleralloc]init];
settingVC.title= @"setting";
NSMutableArray *mArry = [[NSMutableArrayalloc]initWithObjects:homeVC, messageVC, searchVC, settingVC,nil];
for (inti = 0; i < 5; i++) {
UIViewController *vc = [[UIViewControlleralloc]init];
vc.title= [NSStringstringWithFormat:@"第%d个控制器",i+5];
vc.view.backgroundColor= [UIColorcolorWithRed:arc4random()%10*0.1green:(arc4random()%10*0.1)blue:arc4random()%10*0.1alpha:1];
[mArry addObject:vc];
}
//创建标签控制器
UITabBarController *tabBarCtrl = [[UITabBarControlleralloc]init];
tabBarCtrl.viewControllers= mArry;
[self.windowsetRootViewController:tabBarCtrl];
HomeViewController *homeVC = [[HomeViewControlleralloc]init];
homeVC.title= @"home";
//最好不要在这里设置视图控制器的title,这样会破坏它的封装性,要在它自己的.m文件中设置
MessageViewController *messageVC = [[MessageViewControlleralloc]init];
messageVC.title= @"message";
SearchViewController *searchVC = [[SearchViewControlleralloc]init];
searchVC.title= @"search";
SettingViewController *settingVC = [[SettingViewControlleralloc]init];
settingVC.title= @"setting";
NSMutableArray *mArry = [[NSMutableArrayalloc]initWithObjects:homeVC, messageVC, searchVC, settingVC,nil];
for (inti = 0; i < 5; i++) {
UIViewController *vc = [[UIViewControlleralloc]init];
vc.title= [NSStringstringWithFormat:@"第%d个控制器",i+5];
vc.view.backgroundColor= [UIColorcolorWithRed:arc4random()%10*0.1green:(arc4random()%10*0.1)blue:arc4random()%10*0.1alpha:1];
[mArry addObject:vc];
}
//创建标签控制器
UITabBarController *tabBarCtrl = [[UITabBarControlleralloc]init];
tabBarCtrl.viewControllers= mArry;
[self.windowsetRootViewController:tabBarCtrl];
二、标签栏(TabBar)的设置
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Override point for customization after application launch.
CGFloat scrrenWidth = [UIScreenmainScreen].bounds.size.width;
// CGFloat scrrenHeight = [UIScreen mainScreen].bounds.size.height;
//=============================================
[[UIApplicationsharedApplication]registerUserNotificationSettings:[[UIUserNotificationSettingsalloc]init]];
[UIApplicationsharedApplication].applicationIconBadgeNumber= 5;
//=============================================
//*******创建window窗口*********************
//拿到屏幕的大小
CGRect rect = [UIScreenmainScreen].bounds;
//创建一个window
self.window= [[UIWindowalloc]initWithFrame:rect];
//设置窗口颜色
self.window.backgroundColor= [UIColorwhiteColor];
//把当前的window作为程序的主window显示出来
[self.windowmakeKeyAndVisible];
//创建几个视图控制器
HomeViewController *homeVC = [[HomeViewControlleralloc]init];
MessageViewController *messageVC = [[MessageViewControlleralloc]init];
SearchViewController *searchVC = [[SearchViewControlleralloc]init];
SettingViewController *settingVC = [[SettingViewControlleralloc]init];
//创建UITabBarController
UITabBarController *tabbarCtrl = [[UITabBarControlleralloc]init];
tabbarCtrl.viewControllers= @[homeVC, messageVC, searchVC, settingVC];
//使用自定义方式定义tabBarItem
UITabBarItem *item1 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemFavoritestag:10];
homeVC.tabBarItem= item1;
UITabBarItem *item2 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarkstag:11];
item2.badgeValue= @"haha";
messageVC.tabBarItem= item2;
UITabBarItem *item3 = [[UITabBarItemalloc]initWithTitle:@"搜索"image:[UIImageimageNamed:@"tabbar_discover.png"]selectedImage:[UIImageimageNamed:@"tabbar_discover_highlighted.png"]];
searchVC.tabBarItem= item3;
UITabBarItem *item4 = [[UITabBarItemalloc]initWithTitle:@"联系人"image:[UIImageimageNamed:@"tabbar_profile.png"]selectedImage:[UIImageimageNamed:@"tabbar_profile_highlighted.png"]];
settingVC.tabBarItem= item4;
//设置tabBar的背景图片
UIImage *img = [UIImageimageNamed:@"navbg.png"];
UIGraphicsBeginImageContext(CGSizeMake(scrrenWidth,49));
[img drawInRect:CGRectMake(0,0, scrrenWidth,49)];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
tabbarCtrl.tabBar.backgroundImage= img;
//设置tabBar的背景颜色
// tabbarCtrl.tabBar.barTintColor = [UIColor cyanColor];
//设置选中图片的颜色
tabbarCtrl.tabBar.tintColor= [UIColorcyanColor];
//设置选中item后,显示在此item下面的图片
tabbarCtrl.tabBar.selectionIndicatorImage= [UIImageimageNamed:@"选中.png"];
self.window.rootViewController= tabbarCtrl;
return YES;
}
// Override point for customization after application launch.
CGFloat scrrenWidth = [UIScreenmainScreen].bounds.size.width;
// CGFloat scrrenHeight = [UIScreen mainScreen].bounds.size.height;
//=============================================
[[UIApplicationsharedApplication]registerUserNotificationSettings:[[UIUserNotificationSettingsalloc]init]];
[UIApplicationsharedApplication].applicationIconBadgeNumber= 5;
//=============================================
//*******创建window窗口*********************
//拿到屏幕的大小
CGRect rect = [UIScreenmainScreen].bounds;
//创建一个window
self.window= [[UIWindowalloc]initWithFrame:rect];
//设置窗口颜色
self.window.backgroundColor= [UIColorwhiteColor];
//把当前的window作为程序的主window显示出来
[self.windowmakeKeyAndVisible];
//创建几个视图控制器
HomeViewController *homeVC = [[HomeViewControlleralloc]init];
MessageViewController *messageVC = [[MessageViewControlleralloc]init];
SearchViewController *searchVC = [[SearchViewControlleralloc]init];
SettingViewController *settingVC = [[SettingViewControlleralloc]init];
//创建UITabBarController
UITabBarController *tabbarCtrl = [[UITabBarControlleralloc]init];
tabbarCtrl.viewControllers= @[homeVC, messageVC, searchVC, settingVC];
//使用自定义方式定义tabBarItem
UITabBarItem *item1 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemFavoritestag:10];
homeVC.tabBarItem= item1;
UITabBarItem *item2 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarkstag:11];
item2.badgeValue= @"haha";
messageVC.tabBarItem= item2;
UITabBarItem *item3 = [[UITabBarItemalloc]initWithTitle:@"搜索"image:[UIImageimageNamed:@"tabbar_discover.png"]selectedImage:[UIImageimageNamed:@"tabbar_discover_highlighted.png"]];
searchVC.tabBarItem= item3;
UITabBarItem *item4 = [[UITabBarItemalloc]initWithTitle:@"联系人"image:[UIImageimageNamed:@"tabbar_profile.png"]selectedImage:[UIImageimageNamed:@"tabbar_profile_highlighted.png"]];
settingVC.tabBarItem= item4;
//设置tabBar的背景图片
UIImage *img = [UIImageimageNamed:@"navbg.png"];
UIGraphicsBeginImageContext(CGSizeMake(scrrenWidth,49));
[img drawInRect:CGRectMake(0,0, scrrenWidth,49)];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
tabbarCtrl.tabBar.backgroundImage= img;
//设置tabBar的背景颜色
// tabbarCtrl.tabBar.barTintColor = [UIColor cyanColor];
//设置选中图片的颜色
tabbarCtrl.tabBar.tintColor= [UIColorcyanColor];
//设置选中item后,显示在此item下面的图片
tabbarCtrl.tabBar.selectionIndicatorImage= [UIImageimageNamed:@"选中.png"];
self.window.rootViewController= tabbarCtrl;
return YES;
}
三、创建子控制器
#import"MainTabBarController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "SearchViewController.h"
#import "SettingViewController.h"
#import "MoreViewController.h"
@interfaceMainTabBarController()
@end
@implementationMainTabBarController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
//隐藏自己的(系统的)tabBarView
self.tabBar.hidden= YES;
//创建自定义的tabBarView
[self_initViews];
//创建子控制器
[self_initViewControllers];
}
- (void)_initViews{
CGFloat width = [UIScreenmainScreen].bounds.size.width;
CGFloat height = [UIScreenmainScreen].bounds.size.height;
_tabBarImgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0, height- 49, width, 49)];
_tabBarImgView.image= [UIImageimageNamed:@"navbg.png"];
[self.viewaddSubview:_tabBarImgView];
_tabBarImgView.userInteractionEnabled= true;
//创建选中视图
UIImageView *selectedImgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,53,45)];
selectedImgView.tag= 100;
selectedImgView.image= [UIImageimageNamed:@"选中.png"];
//创建5个按钮
for (inti = 0; i < 5; i++) {
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame= CGRectMake(width/ 5 * i, 0, width / 5, 49);
btn.tag= 200 + i;
NSString *imgName = [NSStringstringWithFormat:@"%d",i + 1];
[btn setImage:[UIImageimageNamed:imgName]forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];
[_tabBarImgViewaddSubview:btn];
if (i == 0) {
selectedImgView.center= btn.center;
}
}
[_tabBarImgViewaddSubview:selectedImgView];
}
- (void)buttonClick:(UIButton*)btn{
//设置选择的控制器
self.selectedIndex= btn.tag- 200;
UIView *selectedView = [_tabBarImgViewviewWithTag:100];
[UIViewanimateWithDuration:0.2
animations:^{
selectedView.center= btn.center;
}];
}
- (void)_initViewControllers{
HomeViewController *homeVC = [[HomeViewControlleralloc]init];
MessageViewController *messageVC =[[MessageViewControlleralloc]init];
SearchViewController *searchVC = [[SearchViewControlleralloc]init];
SettingViewController *settingVC = [[SettingViewControlleralloc]init];
MoreViewController *moreVC = [[MoreViewControlleralloc]init];
self.viewControllers= @[homeVC, messageVC, searchVC, settingVC, moreVC];
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "SearchViewController.h"
#import "SettingViewController.h"
#import "MoreViewController.h"
@interfaceMainTabBarController()
@end
@implementationMainTabBarController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
//隐藏自己的(系统的)tabBarView
self.tabBar.hidden= YES;
//创建自定义的tabBarView
[self_initViews];
//创建子控制器
[self_initViewControllers];
}
- (void)_initViews{
CGFloat width = [UIScreenmainScreen].bounds.size.width;
CGFloat height = [UIScreenmainScreen].bounds.size.height;
_tabBarImgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0, height- 49, width, 49)];
_tabBarImgView.image= [UIImageimageNamed:@"navbg.png"];
[self.viewaddSubview:_tabBarImgView];
_tabBarImgView.userInteractionEnabled= true;
//创建选中视图
UIImageView *selectedImgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,53,45)];
selectedImgView.tag= 100;
selectedImgView.image= [UIImageimageNamed:@"选中.png"];
//创建5个按钮
for (inti = 0; i < 5; i++) {
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame= CGRectMake(width/ 5 * i, 0, width / 5, 49);
btn.tag= 200 + i;
NSString *imgName = [NSStringstringWithFormat:@"%d",i + 1];
[btn setImage:[UIImageimageNamed:imgName]forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];
[_tabBarImgViewaddSubview:btn];
if (i == 0) {
selectedImgView.center= btn.center;
}
}
[_tabBarImgViewaddSubview:selectedImgView];
}
- (void)buttonClick:(UIButton*)btn{
//设置选择的控制器
self.selectedIndex= btn.tag- 200;
UIView *selectedView = [_tabBarImgViewviewWithTag:100];
[UIViewanimateWithDuration:0.2
animations:^{
selectedView.center= btn.center;
}];
}
- (void)_initViewControllers{
HomeViewController *homeVC = [[HomeViewControlleralloc]init];
MessageViewController *messageVC =[[MessageViewControlleralloc]init];
SearchViewController *searchVC = [[SearchViewControlleralloc]init];
SettingViewController *settingVC = [[SettingViewControlleralloc]init];
MoreViewController *moreVC = [[MoreViewControlleralloc]init];
self.viewControllers= @[homeVC, messageVC, searchVC, settingVC, moreVC];
}
@end
四、标签控制器和导航控制器的结合
#import"MainTabBarController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "SearchViewController.h"
#import "SettingViewController.h"
#import "MoreViewController.h"
@interfaceMainTabBarController()
{
CGFloat width;
CGFloat height;
}
@end
@implementationMainTabBarController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
//隐藏自己的(系统的)tabBarView
self.tabBar.hidden= YES;
//创建自定义的tabBarView
[self_initViews];
//创建子控制器
[self_initViewControllers];
}
- (void)_initViews{
width = [UIScreenmainScreen].bounds.size.width;
height = [UIScreenmainScreen].bounds.size.height;
_tabBarImgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,height-49,width,49)];
_tabBarImgView.image= [UIImageimageNamed:@"navbg.png"];
[self.viewaddSubview:_tabBarImgView];
_tabBarImgView.userInteractionEnabled= true;
//创建选中视图
UIImageView *selectedImgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,53,45)];
selectedImgView.tag= 100;
selectedImgView.image= [UIImageimageNamed:@"选中.png"];
//创建5个按钮
for (inti = 0; i < 5; i++) {
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame= CGRectMake(width/5 * i, 0, width / 5, 49);
btn.tag= 200 + i;
NSString *imgName = [NSStringstringWithFormat:@"%d",i + 1];
[btn setImage:[UIImageimageNamed:imgName]forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];
[_tabBarImgViewaddSubview:btn];
if (i == 0) {
selectedImgView.center= btn.center;
}
}
[_tabBarImgViewaddSubview:selectedImgView];
}
- (void)buttonClick:(UIButton*)btn{
//设置选择的控制器
self.selectedIndex= btn.tag- 200;
UIView *selectedView = [_tabBarImgViewviewWithTag:100];
[UIViewanimateWithDuration:0.2
animations:^{
selectedView.center= btn.center;
}];
}
- (void)_initViewControllers{
NSLog(@"控制器");
//三级控制器
HomeViewController *homeVC = [[HomeViewControlleralloc]init];
MessageViewController *messageVC =[[MessageViewControlleralloc]init];
SearchViewController *searchVC = [[SearchViewControlleralloc]init];
SettingViewController *settingVC = [[SettingViewControlleralloc]init];
MoreViewController *moreVC = [[MoreViewControlleralloc]init];
NSArray *viewCtrls =@[homeVC, messageVC, searchVC, settingVC, moreVC];
NSMutableArray *navs = [NSMutableArrayarray];
//二级控制器
for (inti = 0; i < viewCtrls.count; i ++) {
UINavigationController *nav = [[UINavigationControlleralloc]initWithRootViewController:viewCtrls[i]];
nav.delegate= self;
[navs addObject:nav];
}
//一级控制器
self.viewControllers= navs;
}
#pragma mark-<UINavigationControllerDelegate>
- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController*)viewController animated:(BOOL)animated{
//当导航控制器的数量等于2时,隐藏标签栏
NSInteger count = navigationController.viewControllers.count;
NSLog(@"%ld", count);
if (count == 2) {
// _tabBarImgView.hidden = YES;
[UIViewanimateWithDuration:0.25animations:^{
_tabBarImgView.frame= CGRectMake(- width, height - 49, width, 49);
}];
}elseif (count == 1) {
// tabBarImgView.hidden = NO;
[UIViewanimateWithDuration:0.25animations:^{
_tabBarImgView.frame= CGRectMake(0,height- 49, width, 49);
}];
}
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "SearchViewController.h"
#import "SettingViewController.h"
#import "MoreViewController.h"
@interfaceMainTabBarController()
{
CGFloat width;
CGFloat height;
}
@end
@implementationMainTabBarController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
//隐藏自己的(系统的)tabBarView
self.tabBar.hidden= YES;
//创建自定义的tabBarView
[self_initViews];
//创建子控制器
[self_initViewControllers];
}
- (void)_initViews{
width = [UIScreenmainScreen].bounds.size.width;
height = [UIScreenmainScreen].bounds.size.height;
_tabBarImgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,height-49,width,49)];
_tabBarImgView.image= [UIImageimageNamed:@"navbg.png"];
[self.viewaddSubview:_tabBarImgView];
_tabBarImgView.userInteractionEnabled= true;
//创建选中视图
UIImageView *selectedImgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,53,45)];
selectedImgView.tag= 100;
selectedImgView.image= [UIImageimageNamed:@"选中.png"];
//创建5个按钮
for (inti = 0; i < 5; i++) {
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame= CGRectMake(width/5 * i, 0, width / 5, 49);
btn.tag= 200 + i;
NSString *imgName = [NSStringstringWithFormat:@"%d",i + 1];
[btn setImage:[UIImageimageNamed:imgName]forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];
[_tabBarImgViewaddSubview:btn];
if (i == 0) {
selectedImgView.center= btn.center;
}
}
[_tabBarImgViewaddSubview:selectedImgView];
}
- (void)buttonClick:(UIButton*)btn{
//设置选择的控制器
self.selectedIndex= btn.tag- 200;
UIView *selectedView = [_tabBarImgViewviewWithTag:100];
[UIViewanimateWithDuration:0.2
animations:^{
selectedView.center= btn.center;
}];
}
- (void)_initViewControllers{
NSLog(@"控制器");
//三级控制器
HomeViewController *homeVC = [[HomeViewControlleralloc]init];
MessageViewController *messageVC =[[MessageViewControlleralloc]init];
SearchViewController *searchVC = [[SearchViewControlleralloc]init];
SettingViewController *settingVC = [[SettingViewControlleralloc]init];
MoreViewController *moreVC = [[MoreViewControlleralloc]init];
NSArray *viewCtrls =@[homeVC, messageVC, searchVC, settingVC, moreVC];
NSMutableArray *navs = [NSMutableArrayarray];
//二级控制器
for (inti = 0; i < viewCtrls.count; i ++) {
UINavigationController *nav = [[UINavigationControlleralloc]initWithRootViewController:viewCtrls[i]];
nav.delegate= self;
[navs addObject:nav];
}
//一级控制器
self.viewControllers= navs;
}
#pragma mark-<UINavigationControllerDelegate>
- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController*)viewController animated:(BOOL)animated{
//当导航控制器的数量等于2时,隐藏标签栏
NSInteger count = navigationController.viewControllers.count;
NSLog(@"%ld", count);
if (count == 2) {
// _tabBarImgView.hidden = YES;
[UIViewanimateWithDuration:0.25animations:^{
_tabBarImgView.frame= CGRectMake(- width, height - 49, width, 49);
}];
}elseif (count == 1) {
// tabBarImgView.hidden = NO;
[UIViewanimateWithDuration:0.25animations:^{
_tabBarImgView.frame= CGRectMake(0,height- 49, width, 49);
}];
}
}
@end
0 0
- 【UI初级------连载七】标签控制器
- UI初级连载八------------标签控制器
- UI导航控制器-----连载七
- UI初级第六课 Tab 控制器(标签控制器)——iOS学习连载21
- iOS UI初级-标签控制器
- 【UI初级--连载五】---------UIViewController(视图控制器)
- 【UI初级--连载六】---------导航控制器(UINavigationController)
- UI初级第四课 视图控制器——iOS学习连载18
- UI初级第五课 导航控制器——iOS学习连载20
- 【UI初级 连载四】--------UI常用控件
- UI初级连载九----------UITableView的使用
- 【UI初级 连载一】------iPhone开发入门
- UI初级连载十一-------触摸与手势
- UI初级连载12-------------滑动视图
- iOS UI初级-视图控制器UIViewController
- IOS--UI--LessonUITabBarController 标签控制器
- 【UI初级 连载三】--------UIWindow 和 UIView 的使用
- 【UI初级--连载八】-------------表视图(UITableView)
- 带碰撞检测的摄像机
- 杭电acm--2052
- [leetcode-284]Peeking Iterator(java)
- 3D GAME PROGRAMMING WITH DIRECTX11(4)
- Lombok -- 简化你的Java编码 (JDK6+)
- 【UI初级------连载七】标签控制器
- HDU 1698 Just a Hook
- 关于Java中的封装、多态、继承(1)
- [BZOJ1046] [HAOI2007]上升序列
- 【项目】——B/S开发中的Static陷阱
- CPU工作原理
- 界面切换之元素共享
- TCP/IP的工作方式
- 剑指Offer第七章面试题(Java版)