码迷,mamicode.com
首页 > 移动开发 > 详细

ios基础篇(八)——UITabBarController的简单介绍

时间:2015-12-11 18:25:09      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

一、简介

UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例子就是QQ、微信、微博等应?。

技术分享技术分享

二、UITabBarController的使用

1、首先初始化UITabBarController

2、设置UIWindow的rootViewController为UITabBarController

3、创建相应的子控制器(viewcontroller)

4、把子控制器添加到UITabBarController

代码:

//
//  AppDelegate.m
//  微信
//
//  Created by Oran Wu on 15-11-5.
//  Copyright (c) 2015年 Xinxin. All rights reserved.
//

#import "AppDelegate.h"
#import "WeChatViewController.h"
#import "AddressBookViewController.h"
#import "FindViewController.h"
#import "MyViewController.h"


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //创建Window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    //创建子控制器
    WeChatViewController *WeChatVC = [[WeChatViewController alloc] init];
    AddressBookViewController *AddressBookVC = [[AddressBookViewController alloc] init];
    FindViewController *FindVC = [[FindViewController alloc] init];
    MyViewController *MyVC = [[MyViewController alloc] init];
    
    //设置UITabBarButton的图片(normal)与选中时图片(selected)
    UIImage *WeChatImage = [UIImage imageNamed:@"tabbar_mainframe"];
    UIImage *WeChatSeletedImage = [UIImage imageNamed:@"tabbar_mainframeHL"];
    WeChatVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"微信" image:WeChatImage selectedImage:WeChatSeletedImage];
    
    UIImage *AddressImage = [UIImage imageNamed:@"tabbar_contacts"];
    UIImage *AddressSeletedImage = [UIImage imageNamed:@"tabbar_contactsHL"];
    AddressBookVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"通讯录" image:AddressImage selectedImage:AddressSeletedImage];
    
    UIImage *FineImage = [UIImage imageNamed:@"tabbar_discover"];
    UIImage *FineSeletedImage = [UIImage imageNamed:@"tabbar_discoverHL"];
    FindVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:FineImage selectedImage:FineSeletedImage];
    //提醒数字
FindVC.tabBarItem.badgeValue
= @"1"; UIImage *MyImage = [UIImage imageNamed:@"tabbar_me"]; UIImage *MySeletedImage = [UIImage imageNamed:@"tabbar_meHL"]; MyVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:MyImage selectedImage:MySeletedImage]; //初始化各ViewController的导航控制器 UINavigationController *WeChatNav = [[UINavigationController alloc] initWithRootViewController:WeChatVC]; UINavigationController *AddressBookNav = [[UINavigationController alloc] initWithRootViewController:AddressBookVC]; UINavigationController *FindNav = [[UINavigationController alloc] initWithRootViewController:FindVC]; UINavigationController *MyNav = [[UINavigationController alloc] initWithRootViewController:MyVC]; //初始化一个控制器 UITabBarController *TabBarCtrl = [[UITabBarController alloc] init]; TabBarCtrl.viewControllers = [NSArray arrayWithObjects:WeChatNav,AddressBookNav,FindNav,MyNav, nil]; //设置控制器为Window的根控制器 self.window.rootViewController = TabBarCtrl; //设置window的背景颜色 self.window.backgroundColor = [UIColor whiteColor]; //设置Window为主窗口并显示出来 [self.window makeKeyAndVisible]; // Override point for customization after application launch. return YES; }

技术分享

 

三、UITabBar

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

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

在上面的程序中,UITabBarController有4个子控制器,所以UITabBar中有4个UITabBarButton,UITabBar的结构?大致如下图所示:

技术分享

技术分享

 

ios基础篇(八)——UITabBarController的简单介绍

标签:

原文地址:http://www.cnblogs.com/0320y/p/5039682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!