美文网首页
用代码创建UITabBarController

用代码创建UITabBarController

作者: 143db5b5572a | 来源:发表于2016-07-02 23:51 被阅读0次

    第一种不需要自定义tabBar  可以在按下面方法创建

    /  Copyright (c) 2014年itcast. All rights reserved.

    //

    #import"CZAppDelegate.h"

    #import"CZOneViewController.h"

    #import"CZTwoViewController.h"

    @implementationCZAppDelegate

    /**

    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= [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

    self.window.backgroundColor= [UIColorwhiteColor];

    UITabBarController*tab = [[UITabBarControlleralloc]init];

    //第一种方式

    CZOneViewController*one = [[CZOneViewControlleralloc]init];

    one.title=@"第一个";

    //设置图像

    one.tabBarItem.image= [UIImageimageNamed:@"tab_buddy_nor"];

    //设置数字

    one.tabBarItem.badgeValue=@"100";

    CZTwoViewController*two = [[CZTwoViewControlleralloc]init];

    two.tabBarItem.image= [UIImageimageNamed:@"tab_me_nor"];

    two.title=@"第二个";

    //    [tab addChildViewController:one];

    //    [tab addChildViewController:two];

    tab.viewControllers=@[one, two];

    self.window.rootViewController= tab;

    [self.windowmakeKeyAndVisible];

    returnYES;

    第二种如果要自定义tabBar

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

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

    #import"XZAppDelegate.h"

    #import"XZTabBarViewController.h"

    @implementationXZAppDelegate

    - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

    {

    self.window= [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

    self.window.backgroundColor= [UIColorwhiteColor];

    XZTabBarViewController*tab = [[XZTabBarViewControlleralloc]init];

    self.window.rootViewController= tab;

    [self.windowmakeKeyAndVisible];

    returnYES;

    }

    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"

    @interfaceXZTabBarViewController()

    @property(nonatomic,strong)UIButton*selectedButton;

    @end

    @implementationXZTabBarViewController

    - (void)loadView

    {

    [superloadView];

    //创建子控制器

    XZOneViewController*one = [[XZOneViewControlleralloc]init];

    XZTwoViewController*two = [[XZTwoViewControlleralloc]init];

    XZThreeViewController*thr = [[XZThreeViewControlleralloc]init];

    thr.view.backgroundColor= [UIColorpurpleColor];

    XZFrourViewController* fro = [[XZFrourViewControlleralloc]init];

    XZFiveViewController*fiv = [[XZFiveViewControlleralloc]init];

    //添加到tabBar的子控制器数组中它会按添加先后顺序排列

    // ****注意要先子控制器再自定义他的按钮添加到tabBar上

    // tabBar是UITabBarController下的整个工具条

    // UITabBarButton是tabBar工具条中的每一个按钮

    //每一个UITabBarButton显示什么内容由,由对应子控制器的tabBarItem属性来决定

    //详见**UITabBarController简单介绍**

    self.viewControllers=@[one, two, thr, fro, fiv];

    CGRecttab =self.tabBar.bounds;

    CGFloatw = tab.size.width/5;

    CGFloath = tab.size.height;

    CGFloaty =0;

    for(inti =0; i <5; i++) {

    UIButton*btn = [[UIButtonalloc]init];

    CGFloatx = i * w;

    btn.frame=CGRectMake(x, y, w, h);

    NSString*imageName = [NSStringstringWithFormat:@"TabBar%d", i+1];

    NSString*imageSelName = [imageNamestringByAppendingString:@"Sel"];

    [btnsetBackgroundImage:[UIImageimageNamed:imageName]forState:UIControlStateNormal];

    [btnsetBackgroundImage:[UIImageimageNamed:imageSelName]forState:UIControlStateSelected];

    [self.tabBaraddSubview:btn];

    btn.tag= i;

    [btnaddTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchDown];

    if(i ==0) {

    [selfclick: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

          本文链接:https://www.haomeiwen.com/subject/ahnqjttx.html