您的位置:首页 > 产品设计 > UI/UE

UITabBarController

2015-10-31 16:11 267 查看
//

// AppDelegate.m

// UI_Lesson_19

//

// Created by xalo on 15/9/29.

// Copyright © . All rights reserved.

//

#import "AppDelegate.h"

#import "BaseViewController.h"

#import "FirstViewController.h"

#import "SecondViewController.h"

#import "ThreedViewController.h"

#import "ForthViewController.h"

#import "FifthViewController.h"

#import "BaseTabBarController.h"

@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate

- (void)dealloc

{

[_window release];

[super dealloc];

}

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

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

// Override point for customization after application launch.

self.window.backgroundColor =
[UIColor whiteColor];

[self.window makeKeyAndVisible];

/*

FirstViewController *firstController = [[[FirstViewController alloc]init] autorelease];

UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:firstController];

SecondViewController *secondController = [[[SecondViewController alloc]init] autorelease];

UINavigationController *navigation2 = [[UINavigationController alloc]initWithRootViewController:secondController];

ThreedViewController *threeController = [[[ThreedViewController alloc]init] autorelease];

UINavigationController *navigation3 = [[UINavigationController alloc]initWithRootViewController:threeController];

ForthViewController *forthController = [[[ForthViewController alloc]init] autorelease];

UINavigationController *navigation4 = [[UINavigationController alloc]initWithRootViewController:forthController];

FifthViewController *fifthController = [[[FifthViewController alloc]init] autorelease];

UINavigationController *navigation5 = [[UINavigationController alloc]initWithRootViewController:fifthController];

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

//设置其viewControllers数组

tabBaeController.viewControllers = @[navigation,navigation2,navigation3,navigation4,navigation5];

//window的根视图控制器被设置为标签控制器

self.window.rootViewController = tabBaeController;

[navigation release];

[tabBaeController release];

*/

#pragma appearance任何位置都可以设置 但设置之后类型全部改变

//创建标签栏之前调用appearance协议修改tabbar的默认值 (navigation也可以)

//appearance协支持我们修改属性的默认值 然后对应创建的对象就会以此为依据

[[UITabBar appearance]setBarTintColor:[UIColor purpleColor]];

[[UINavigationBar appearance]setBarTintColor:[UIColor orangeColor]];

[[UINavigationBar appearance]setTranslucent:NO];

[[UITabBar appearance]setTranslucent:NO];

self.window.rootViewController =
[[[BaseTabBarController alloc]init]autorelease];

return YES;

}

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

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS
message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

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

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state
in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

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

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

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

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user
interface.

}

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

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end

//

// BaseTabBarController.h

// UI_Lesson_19

//

// Created by xalo on 15/9/29.

// Copyright © 2015年 蓝鸥科技.
All rights reserved.

//

#import <UIKit/UIKit.h>

@interface BaseTabBarController : UITabBarController

@end

//

// BaseTabBarController.m

// UI_Lesson_19

//

// Created by xalo on 15/9/29.

// Copyright © 2015年 蓝鸥科技.
All rights reserved.

//

#import "BaseTabBarController.h"

#import "PublicHeader.h"

@interface BaseTabBarController ()

//多态封装

//通过类名获得视图控制器

- (UIViewController *)viewControllerWithClassName:(NSString *)className;//根据类名创建视图控制器 并返回

//为导航控制器指定根视图控制器

- (UINavigationController *)navigationControllerWithRootViewControler:(UIViewController *)rootViewController;//创建一个带有指定根视图控制器对象的导航控制器

@end

@implementation BaseTabBarController

//实现私有方法

- (UIViewController *)viewControllerWithClassName:(NSString *)className{

Class currentClass = NSClassFromString(className);//将字符串转为 类型名

return [[[currentClass alloc]init ]autorelease];//得到的是视图控制器类型的子类 多态

}

- (UINavigationController *)navigationControllerWithRootViewControler:(UIViewController *)rootViewController{

return [[[UINavigationController alloc]initWithRootViewController:rootViewController] autorelease];

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

NSMutableArray *customViewController = [NSMutableArray array];

//用于保存创建好的视图控制器

NSArray *viewControllerNames = @[@"FirstViewController",@"SecondViewController",@"ThreedViewController",@"ForthViewController",@"FifthViewController"];

//视图控制器的类型名数组

for (int i
= 0 ; i < viewControllerNames.count;
i ++) {

//1.调用方法一 将字符串转化为类型名 得到视图控制器的类

UIViewController *viewController = [self viewControllerWithClassName:viewControllerNames[i]];

//2. 调用方法二 将转化得到的viewController的子类作为导航控制器的根视图控制器

UINavigationController *navigationController = [self navigationControllerWithRootViewControler:viewController];

//3. 将添加了根视图控制器的导航控制器添加到可变数组中 准备交给标签控制器管理

[customViewController addObject:navigationController];

}

//将视图控制器数组指定给viewControllers属性

//标签控制器的管理方式是将要管理的视图控制器加入到一个数组中 在这就调用了viewController的视图view(内部调用了loadview 和 viewDidload 两个方法 所有一定要注意方法的先后顺序 否则会出现界面延迟 例如为标签添加图标是要是在 viewDidLoad中添加就会出现延迟显示的情况 因为之前viewDidLoad已经调用过 在没有让它再次显示的情况前不会在调用这个方法 图标也就处于为加载的状态 只有让它出现才会显示图标 所有应该在标签视图控制器将视图加入viewControllers之前为视图控制器设置相关属性 解决办法就是在重写init中进行属性赋值)

self.viewControllers =
customViewController;

//通过修改标签栏的tinntcolor属性可以影响系统样式的标签的选中颜色

self.tabBar.tintColor =
[UIColor orangeColor];

//通过barTintColor可以改变标签栏的表面着色

// self.tabBar.barTintColor = [UIColor blueColor];

//设置tabbar背景图片

// self.tabBar.backgroundImage = [UIImage imageNamed: @"屏幕快照 2015-09-29
16.12.31.png"];

//设置tabbar样式(两种)

self.tabBar.barStyle = UIBarStyleBlack;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

//

// BaseViewController.h

// UI_Lesson_19

//

// Created by xalo on 15/9/29.

// Copyright © 2015年 蓝鸥科技.
All rights reserved.

//

#import <UIKit/UIKit.h>

@interface BaseViewController : UIViewController

@property(nonatomic, retain)UILabel *textLabel;

@end

//

// BaseViewController.m

// UI_Lesson_19

//

// Created by xalo on 15/9/29.

// Copyright © 2015年 蓝鸥科技.
All rights reserved.

//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)dealloc

{

[_textLabel release];

[super dealloc];

}

- (UILabel *)textLabel{

if ( !_textLabel )
{

self.textLabel =
[[[UILabel alloc]initWithFrame:self.view.bounds] autorelease];

_textLabel.backgroundColor =
[UIColor colorWithRed:arc4random()
% 256 / 255.0 green:arc4random()
% 256 / 255.0 blue:arc4random()
% 256 / 255.0 alpha:0.5];

_textLabel.font =
[UIFont boldSystemFontOfSize:100];

_textLabel.adjustsFontSizeToFitWidth = YES;

_textLabel.textColor =
[UIColor whiteColor];

[self.view addSubview:_textLabel];

}

return _textLabel;

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

//

// FirstViewController.h

// UI_Lesson_19

//

// Created by xalo on 15/9/29.

// Copyright © 2015年 蓝鸥科技.
All rights reserved.

//

#import "BaseViewController.h"

@interface FirstViewController : BaseViewController

@end

//

// FirstViewController.m

// UI_Lesson_19

//

// Created by xalo on 15/9/29.

// Copyright © 2015年 蓝鸥科技.
All rights reserved.

//

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.textLabel.text = @"第一个视图控制器";

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];

[self.view addGestureRecognizer:tap];

[tap release];

}

- (void)handleTap:(UITapGestureRecognizer *)sender{

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

#pragma 设置要在PUSH之前

//给要显示 又不让显示tabBar的视图控制器把hidesBottomBarWhenPushed 设置为YES

//要被入栈显示的视图控制器 如果不需要标签栏显示 则需要在push之前将hidesBottomBarWhenPushed 属性设置为YES 当其pop返回上一个视图控制器的时候不会影响该视图控制器的显示标签栏

viewControllern.hidesBottomBarWhenPushed = YES;

[self.navigationController pushViewController:viewControllern animated:YES];

[viewControllern release];

}

- (instancetype)init{

if(self =
[super init]){

//初始化方法中添加标签图标
(在创建对象是就创建标签图标, 别的时间点会出现延迟加载)

// self.tabBarItem = [[[UITabBarItem alloc]initWithTabBarSystemItem:0 tag:0] autorelease] ;

// self.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页"
image:[[UIImage imageNamed:@"1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:30];

//为标签加图片是 为了防止系统着色 需要将图片对象渲染模式进行修改

self.tabBarItem =
[[[UITabBarItem alloc]initWithTitle:@"首页" image:[[UIImage imageNamed:@"iconfont-hot-2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"iconfont-hotfill.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]]autorelease];

//右上角 红色 字符串 主要用作当前视图的提示信息 绝大多数是数字字符串 如购物车上的提示 听话记录上的未接来电数量

self.tabBarItem.badgeValue = @"1个SB";

}

return self;

}

- (void)viewWillDisappear:(BOOL)animated{

[super viewWillDisappear:animated];

self.tabBarItem.badgeValue = nil;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: