您的位置:首页 > 产品设计 > UI/UE

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 呢? 哪位高手给解释下啊
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: