第一種不須要自定義tabBar 能夠在按下面方法建立數組
//
#import "CZAppDelegate.h"
#import "CZOneViewController.h"
#import "CZTwoViewController.h"
@implementation CZAppDelegate
/**
iOS7 view的frame: (0,0,320,480)
iOS6 view的frame: (0,0,320,411) 從狀態欄下方開始計算座標
UITabBar高度是49
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
UITabBarController *tab = [[UITabBarController alloc] init];
// 第一種方式
CZOneViewController *one = [[CZOneViewController alloc] init];
one.title = @"第一個";
// 設置圖像
one.tabBarItem.image = [UIImage imageNamed:@"tab_buddy_nor"];
// 設置數字
one.tabBarItem.badgeValue = @"100";
CZTwoViewController *two = [[CZTwoViewController alloc] init];
two.tabBarItem.image = [UIImage imageNamed:@"tab_me_nor"];
two.title = @"第二個";
// [tab addChildViewController:one];
// [tab addChildViewController:two];
tab.viewControllers = @[one, two];
self.window.rootViewController = tab;
[self.window makeKeyAndVisible];
return YES;
app
第二種若是要自定義tabBar工具
1.先建立一個繼承UITabBarController的控制器 atom
2.設置UIWindow的根控制器爲 繼承UITabBarController的控制器spa
#import "XZAppDelegate.h"
#import "XZTabBarViewController.h"
@implementation XZAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
XZTabBarViewController *tab = [[XZTabBarViewController alloc] init];
self.window.rootViewController = tab;
[self.window makeKeyAndVisible];
return YES;
}
orm
3.在繼承UITabBarController的控制器中建立子控制器繼承
// XZTabBarViewController.m
// 手碼自定義TabBar
//
// Created by chao on 14-8-19.
// Copyright (c) 2014年 chao. All rights reserved.
//
#import "XZTabBarViewController.h"
#import "XZOneViewController.h"
#import "XZTwoViewController.h"
#import "XZThreeViewController.h"
#import "XZFrourViewController.h"
#import "XZFiveViewController.h"
@interface XZTabBarViewController ()
@property (nonatomic, strong) UIButton *selectedButton;
@end
@implementation XZTabBarViewController
- (void)loadView
{
[super loadView];
// 建立子控制器
XZOneViewController *one = [[XZOneViewController alloc] init];
XZTwoViewController *two = [[XZTwoViewController alloc] init];
XZThreeViewController *thr = [[XZThreeViewController alloc] init];
thr.view.backgroundColor = [UIColor purpleColor];
XZFrourViewController * fro = [[XZFrourViewController alloc] init];
XZFiveViewController *fiv = [[XZFiveViewController alloc] init];
// 添加到tabBar的子控制器數組中 它會按添加前後順序排列
// ****注意要先子控制器 再自定義他的按鈕添加到tabBar 上
// tabBar 是UITabBarController 下的整個工具條
// UITabBarButton 是tabBar工具條 中的每個按鈕
// 每個UITabBarButton 顯示什麼內容由,由對應子控制器的tabBarItem屬性來決定
// 詳見 **UITabBarController簡單介紹**
self.viewControllers = @[one, two, thr, fro, fiv];
CGRect tab = self.tabBar.bounds;
CGFloat w = tab.size.width / 5;
CGFloat h = tab.size.height;
CGFloat y = 0;
for (int i = 0; i < 5; i++) {
UIButton *btn = [[UIButton alloc] init];
CGFloat x = i * w;
btn.frame = CGRectMake(x, y, w, h);
NSString *imageName = [NSString stringWithFormat:@"TabBar%d", i+1];
NSString *imageSelName = [imageName stringByAppendingString:@"Sel"];
[btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:imageSelName] forState:UIControlStateSelected];
[self.tabBar addSubview:btn];
btn.tag = i;
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];
if (i == 0) {
[self click:btn];
}
}
}
- (void)click:(UIButton *)btn
{
if (self.selectedButton == btn) return;
self.selectedButton.selected = NO;
btn.selected = YES;
self.selectedButton = btn;
self.selectedIndex = btn.tag;
}
@end
get