用代码创建UITabBarController

第一种不需要自定义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;


第二种如果要自定义tabBar

1.先创建一个继承UITabBarController的控制器 

2.设置UIWindow的根控制器为 继承UITabBarController的控制器

#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;
}


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


你可能感兴趣的:(用代码创建UITabBarController)