iOS UITabBarController使用方法(欢迎提建议)
2014-12-01 09:53
471 查看
UITabBarController系统自带控件的使用方法:
首先,创建一个demo ,然后:
1. 创建一个继承于 UITabBarController 的类
BaseTabBarViewController.h
#import <UIKit/UIKit.h>
@interface BaseTabBarViewController :UITabBarController
@end
BaseTabBarViewController.m
@implementation BaseTabBarViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
//设置tabBar背景图片
self.tabBar.backgroundImage = [UIImageimageNamed:@"tabbarBgDiv.jpg"];
//加载TabBarController控制器类
[selfinitTabBarViewControllors];
}
- (void)initTabBarViewControllors
{
//初始化
APengViewController *apengVC = [[APengViewController alloc]init];
LoginViewController *loginVC = [[LoginViewControlleralloc]init];
NSArray *rootControllers =@[apengVC,apengVC,loginVC];
//将每个标签栏名字存入数组
NSArray *rootTabBarNames =@[@"标签1",@"标签2",@"标签3"];
//指定数组的数量
NSMutableArray *viewControllers = [[NSMutableArrayalloc]initWithCapacity:4];
//循环设置每个对应的标签栏标题和图片
for(int i =0 ; i < [rootTabBarNamescount];
i++)
{
UINavigationController *navigationViewController = [[UINavigationControlleralloc]initWithRootViewController:[rootControllersobjectAtIndex:i]];
navigationViewController.tabBarItem.title = [rootTabBarNamesobjectAtIndex:i];
//定义两张图片,一张选中时,一张为选中时
UIImage *selectedImage = [UIImageimageNamed:[NSStringstringWithFormat:@"tabbar_select_%d.png",i]];
UIImage *unSelectedImage = [UIImageimageNamed:[NSStringstringWithFormat:@"tabbar_normal_%d.png",i]];
[navigationViewController.tabBarItemsetFinishedSelectedImage:selectedImagewithFinishedUnselectedImage:unSelectedImage];
[viewControllersaddObject:navigationViewController];
}
self.viewControllers = viewControllers;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
最后在appdelegate.m里的:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch.
BaseTabBarViewController *baseVC = [[BaseTabBarViewControlleralloc]init];
//默认设置索引为1的标签控制器
baseVC.selectedIndex =1;
self.window.rootViewController
= baseVC;
returnYES;
}
做ios 也有些日子了,但是好多细节的东西还是没有搞明白,反思了下主要是太过浮躁.... 希望以后能静心修炼 。保持饥饿,保持愚蠢。
实现是可以实现的,但是我这里有个问题: 为什么要用 UINavigationController 呢? 哪位高手给解释下啊
首先,创建一个demo ,然后:
1. 创建一个继承于 UITabBarController 的类
BaseTabBarViewController.h
#import <UIKit/UIKit.h>
@interface BaseTabBarViewController :UITabBarController
@end
BaseTabBarViewController.m
@implementation BaseTabBarViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
//设置tabBar背景图片
self.tabBar.backgroundImage = [UIImageimageNamed:@"tabbarBgDiv.jpg"];
//加载TabBarController控制器类
[selfinitTabBarViewControllors];
}
- (void)initTabBarViewControllors
{
//初始化
APengViewController *apengVC = [[APengViewController alloc]init];
LoginViewController *loginVC = [[LoginViewControlleralloc]init];
NSArray *rootControllers =@[apengVC,apengVC,loginVC];
//将每个标签栏名字存入数组
NSArray *rootTabBarNames =@[@"标签1",@"标签2",@"标签3"];
//指定数组的数量
NSMutableArray *viewControllers = [[NSMutableArrayalloc]initWithCapacity:4];
//循环设置每个对应的标签栏标题和图片
for(int i =0 ; i < [rootTabBarNamescount];
i++)
{
UINavigationController *navigationViewController = [[UINavigationControlleralloc]initWithRootViewController:[rootControllersobjectAtIndex:i]];
navigationViewController.tabBarItem.title = [rootTabBarNamesobjectAtIndex:i];
//定义两张图片,一张选中时,一张为选中时
UIImage *selectedImage = [UIImageimageNamed:[NSStringstringWithFormat:@"tabbar_select_%d.png",i]];
UIImage *unSelectedImage = [UIImageimageNamed:[NSStringstringWithFormat:@"tabbar_normal_%d.png",i]];
[navigationViewController.tabBarItemsetFinishedSelectedImage:selectedImagewithFinishedUnselectedImage:unSelectedImage];
[viewControllersaddObject:navigationViewController];
}
self.viewControllers = viewControllers;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
最后在appdelegate.m里的:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch.
BaseTabBarViewController *baseVC = [[BaseTabBarViewControlleralloc]init];
//默认设置索引为1的标签控制器
baseVC.selectedIndex =1;
self.window.rootViewController
= baseVC;
returnYES;
}
做ios 也有些日子了,但是好多细节的东西还是没有搞明白,反思了下主要是太过浮躁.... 希望以后能静心修炼 。保持饥饿,保持愚蠢。
实现是可以实现的,但是我这里有个问题: 为什么要用 UINavigationController 呢? 哪位高手给解释下啊
相关文章推荐
- iOS 自定义UITabBarView使用方法(欢迎提建议和补充完善)
- iOS 网络请求框架AFNetworking使用方法(欢迎提建议和分享遇到的问题)
- iOS 开发使用半透明模糊效果方法整理(欢迎分享和提建议)
- iOS 图片缓存框架SDWebImage使用方法(欢迎提建议和分享遇到的问题)
- iOS 网络请求框架MKNetworkKit使用方法(欢迎提建议和分享遇到的问题)
- iOS开发UI篇—UITabBarController生命周期(使用storyoard搭建)
- iOS学习之第二个View使用UITabBarViewController
- ios更改UITabBarController背景以及选中背景图片的方法
- [IOS]UITabBarController和UINavigationController结合使用
- iOS 控件之UITextField及键盘挡住处理方法(欢迎大家提建议和分享)
- IOS UINavigationController UITabBarController嵌套使用的时候显示消息数量
- IOS学习之第二个View使用UITabBarViewController
- iOS学习之第二个View使用UITabBarViewController
- iOS学习之第二个View使用UITabBarViewController
- 【深入浅出IOS开发】UITabBarController和UINavigationController混合使用
- iOS基础控件-UITabBarController - 3 之<UITabBarControllerDelegate>协议中的四个重要方法
- iOS开发之transitionFromViewController方法的使用
- ios更改UITabBarController背景以及选中背景图片的方法
- iOS学习之第二个View使用UITabBarViewController
- iOS学习之第二个View使用UITabBarViewController