1.UITabBarController和UINavigationController是系统提供的两种视图控制器, 它们一定程度上代表了程序的流程结构, 他们也可以混合在一起使用, 混合时最好将UINavigationController作为UITabBarController的子控制器;
2.tabBar.translucent=NO时, 子控制器的视图不会被UITabBarController的控制栏遮挡;
3.子控制器的tabBarItem, 会作为UITabBarController的控制栏的一个项来显示. 子控制器的title属性, 与tabBarItem.title属性, 都能影响到控制栏的标题文字;
4.子控制器的tabBarItem的 [setFinishedSelectedImage: withFinishedUnselectedImage:] 方法, 在iOS7中被换成了 [initWithTitle: image: selectedImage:] 方法, iOS7中的UIImage参数需要使用 [imageWithRenderingMode:] 方法加工后, 才能显示正常;
5.子控制器的tabBarItem的选中和未选中图标, 要求尺寸是30x30, 才会正常显示;
创建UITabBarController时
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController* naviController0 = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] initWithTitle:@"Page0"]];
naviController0.navigationBar.translucent = NO; // 可以换成YES测试下效果
UINavigationController* naviController1 = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] initWithTitle:@"Page1"]];
naviController1.navigationBar.translucent = NO;
UINavigationController* naviController2 = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] initWithTitle:@"Page2"]];
naviController2.navigationBar.translucent = NO;
UITabBarController* tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = @[naviController0, naviController1, naviController2];
tabBarController.tabBar.translucent = NO; // 可以换成YES测试下效果
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = tabBarController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
子控制器中
- (id)initWithTitle:(NSString*)title
{
self = [super init];
if (self)
{
// 把这段代码移到 [viewDidLoad] 中, 可以测试下效果
UITabBarItem* tabBarItem = [[UITabBarItem alloc] init];
tabBarItem.badgeValue = @"xyz"; // 右上图标
tabBarItem.title = title; // 标题文字
tabBarItem.image = [[UIImage imageNamed:@"fruit-apple0.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // 选中与未选中时的图片, 尺寸必须是30x30
tabBarItem.selectedImage = [[UIImage imageNamed:@"fruit-apple1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // iOS7中的方法
[tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"fruit-apple0.png"]
withFinishedUnselectedImage:[UIImage imageNamed:@"fruit-apple1.png"]]; // iOS6以前的方法
self.title = title;
self.tabBarItem = tabBarItem;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.layer.borderColor = [UIColor redColor].CGColor;
self.view.layer.borderWidth = 2.0;
self.view.layer.masksToBounds = YES;
}