美文网首页
UITabBarController简单介绍

UITabBarController简单介绍

作者: 嗯加了哦屋企啦嗯突击 | 来源:发表于2017-03-03 11:27 被阅读0次

    本文转载自:http://www.cnblogs.com/wendingding/p/3775488.html

    UITabBarController和UINavigationController类似,UITabBarController也可以轻松的管理多个控制权,轻松完成控制器之前的转换,经典的例子就是qq、微信等应用。

    UITabBarController的使用

    1、使用步骤:

    1.1 初始化UITabBarController

    1.2 设置UIWindow的rootViewController为UITabBarController

    1.3 创建响应的子控制器(viewController)

    1.4 把子控制器添加到UITabBarController

    2、示例代码

    2.1 建立一个空的项目File->New->Single View Application, 在Application的代理中编码,例如AppDelegate.m

    额外说明(与本标题不相关,但有点联系):

    如果自定义类需要把Supporting File下的main.m修改为自己的类并在自己的类中继承UIResponder,声明UIApplicationDelegate,并设定一个UIWindow的对象(成员变量、属性)。如

    原main:

    #import <UIKit/UIKit.h>

    #import "AppDelegate.h"

    int main(int argh, char * argue[]){

    @autoreleasepool{

    return UIApplicationMain(argh, argue, nil, NSStringFromClass([AppDelegate class]));

    }

    }

    修改为:

    #import <UIKit/UIKit.h>

    #import "myclass.h"

    int main(int argh, char * argue[]){

    @autoreleasepool {

    return UIApplicationMain(argh, argue, nil, NSStringFromClass([myclass class]));

    }

    }

    原AppDelegate.h

    #import <UIKit/UIKit.h>

    @interface AppDelegate : UIResponder <UIApplicationDelegate>

    @property (strong, nonatomic) UIWindow * window;

    @end

    对应的myclass.h为

    #import <UIKit/UIKit.h>

    @interface myclass : UIResponder <UIApplicationDelegate>

    @property (strong, nonatomic) UIWindow *window;

    @end

    2.2 AppDelegate.m文件内实现:

    #import "AppDelegate.h"

    @interface AppDelegate ()

    @end

    @implementation AppDelegate

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

    //1.创建window

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];

    //2.初始化一个tabBar控制器

    UITabBarController * tb = [[UITabBarController alloc] init];

    //3.设置控制器为window的根控制器

    self.window.rootViewController = tb;

    //4.创建子控制器c1

    UIViewController * c1 = [[UIViewController alloc] init];

    c1.view.backgroundColor = [UIColor grayColor];

    //设定名字

    c1.tabBarItem.title =@"首页";

    //设定选择前图片

    c1.tabBarItem.image = [UIImage imageNamed:@"home-page"];

    //设定选择后图片

    c1.tabBarItem.selectedImage = [UIImage imageNamed:@"home-page_2"];

    //设置图标右上角显示数量,在此为写死,实际应用中应根据取值而定

    c1.tabBarItem.badgeValue = @"123";

    UILabel * lab1 = [[UUILabel alloc] init];

    //设定背景图,即使没有也要设定。否则无法显示(alloc init创建内存、CGRectMake创建大小、backgroundColor指定背景或image指定图片、父类addSubview添加到父类,view缺一不可展现。如果是Controller除外)

    lab1.backgroundColor = [UIColor clearColor];

    lab1.frame = CGRectMake(100, 100, 100, 20);

    lab.text = @"第一个页面";

    [c1.view addSubview:lab];

    //4.创建子控制器c2

    UIViewController * c2 = [[UIViewController alloc]init];

    c2.view.backgroundColor = [UIColor brownColor];

    c2.tabBarItem.title = @"客户录入";

    c2.tabBarItem.image = [UIImage imageNamed:@"User-Icon"];

    c2.tabBarItem.selectedImage = [UIImage imageNamed:@"User-Icon_2"];

    UILabel * lab2 = [[UILabel alloc] init];

    lab2.backgroundColor = [UIColor clearColor];

    lab2.frame = CGRectMake(100, 100, 100, 20);

    lab2.text = @"第二个页面";

    [c2.view addSubview:lab2];

    //4.创建子控制器c3

    UIViewController * c3 = [[UIViewController alloc] init];

    c3.tabBarItem.title = @"我";

    c3.tabBarItem.image = [UIImage imageNamed:@"me"];

    c3.tabBarItem.selectedImage = [UIImage imageNamed:@"me_2"];

    c3.view.backgroundColor = [UIColor greenColor];

    UILabel * lab3 = [[UILabel alloc] init];

    lab3.backgroundColor = [UIColor clearColor];

    lab3.frame CGRectMake(100, 100, 100, 20);

    lab3.text = @"第三个页面";

    [c3.view addSubview:lab3];

    //5. 将子控制器添加到UITabBarController中

    //第一种方式,逐个添加:

    // [tab addChildViewController:c1];

    // [tab addChildViewController:c2];

    // [tab addChildViewController:c3];

    tab.viewControllers = @[c1, c2, c3];

    //第二种方式,数组方式添加

    [self.window makeKeyAndVisible];

    return YES;

    }

    - (void)applicationWillResignActive:(UIApplication *) application{

    }

    - (void)applicationDidEnterBackground:(UIApplication *)application{

    }

    - (void)applicationWillEnterForeground:(UIApplication *)application{

    }

    - (void)applicationDidBecomeActive:(UIApplication *)application{

    }

    - (void)applicationWillTerminate:(UIApplication *)application {

    }

    @end

    //===重要说明=====重要说明=====重要说明==

    1. UITabBar

    下方的工具条称为UITabBar, 如果UITabBarController有N个子控制器,那么UITabBar内部就有有N个UITabBarButton作为子控件与之对应。

    注意:UITabBarButton在UITabBar中的位置是均分的,UITabBar的高度为49。

    在上面的程序中, UITabBarController有3个子控制器(c1,c2,c3)所以UITabBarButton,UITabBar的结构大致均分。

    2. UITabBarButton

    UITabBarButton里面显示什么内容,由对应子控制器的tabBarItem属性来决定

    UITabBarItem有一下属性影响着UITabBarButton的内容

    标题文字

    @property(nonatomic, copy) NSString *title;

    图标

    @property(nonatomic, retain) UIImage *image;

    选中时的图标

    @property(nonatomic, retain) UIImage *selectedImage;

    提醒数字

    @property(nonatomic, copy) NSString *badgeValue;

    3. 有两种方式可以往UITabBarController中添加子控件

    3.1 [tb addChildViewController:c1];

    3.2 tb.viewControllers = @[c1,c2,c3];

    注意:展示的顺序和添加的顺序一致, 和导航控制器不同,展现在眼前的是第一个添加的控制器对应的view

    相关文章

      网友评论

          本文标题:UITabBarController简单介绍

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