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

UI - UITabBarController

2015-10-18 15:03 399 查看
<AppDelegate.m>

#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

RootViewController *tabBarVC = [[RootViewController alloc ]init];
self.window.rootViewController = tabBarVC;

//设置背景图片
//    tabBarVC.tabBar.backgroundImage = [UIImage imageNamed:@"320x49.png"];
//tintcolor   图标图片选中时的颜色
tabBarVC.tabBar.tintColor = [UIColor purpleColor];
//barTintcolor   bar 的背景颜色,如果有背景有图片的话,则不会显示此颜色
tabBarVC.tabBar.barTintColor = [UIColor greenColor];
//给所有的 navigationbar 设置统一的颜色
[[UINavigationBar appearance]setBarTintColor:[UIColor yellowColor]];

//设置代理
tabBarVC.delegate = self;

[tabBarVC release];

return YES;
}
//===================================== UITabBarControllerDelegate 代理的方法================================

//是否可以被选中
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
return YES; //NO时则不可选择下面的标签
}
//选中 controller
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
UINavigationController *naVC = (UINavigationController *)viewController;
//取消 badgeValue
naVC.tabBarItem.badgeValue = nil;

NSLog(@"%@",[naVC.viewControllers firstObject]);
}
//将要自定义 viewcontroller
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
{
for (UINavigationController *naVC in viewControllers) {
NSLog(@"%@",[naVC.viewControllers firstObject]);
}
NSLog(@"将要自定义%@",viewControllers);  //点击 edit 时触发
}

- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{

}
//结束自定义
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
for (UINavigationController *naVC in viewControllers) {
NSLog(@"%@",[naVC.viewControllers firstObject]);
}
NSLog(@"结束自定义%@",viewControllers);  //点击 edit 时触发
}

- (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


<RootViewController.h>

#import <UIKit/UIKit.h>

@interface RootViewController : UITabBarController

@end


<RootViewController.m>

#import "RootViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"
#import "FifthViewController.h"
#import "SixthViewController.h"
#import "SeventhViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

//配置 viewController
[self configViewControllers];

}
/*
UITabBarController 是标签控制器,也是多视图控制器, 只不过它管理的视图控制器没有依赖关系,没有层级关系,是并列关系.所有的视图控制器都是同时存在的,空间不会释放
UITabBarController 也是自带一个 view 和显示内容以及屏幕下方的 tabBar.  对于 TabBar 是一组标签,每一个标签对应一个视图控制器.点击每个标签会进行视图控制器的切换.
*/
//配置 viewController
-(void)configViewControllers
{
//第一个
FirstViewController *firstVC = [[FirstViewController alloc ]init ];
UINavigationController *firstNC = [[UINavigationController alloc]initWithRootViewController:firstVC];
//title
firstNC.tabBarItem.title = @"主页";
//image
firstNC.tabBarItem.image = [UIImage imageNamed:@"53-house.png"];
//badgeValue
firstNC.tabBarItem.badgeValue = @"99";
//设置选中的图片
firstNC.tabBarItem.selectedImage = [UIImage imageNamed:@"icon_home.png"];

//改变 tabbaritem 中的字体
[firstNC.tabBarItem setTitleTextAttributes:@{ NSFontAttributeName : [UIFont fontWithName:@"AmericanTypewriter-Bold" size:13] } forState:UIControlStateNormal];
//只显示文字不显示图片
//要想显示title必须要有image,你可以给image弄张透明的图片。titlePositionAdjustment这个可以调整titile显示的上下左右位置,
firstNC.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -10);

//第二个
SecondViewController *secondVC = [[SecondViewController alloc]init];
UINavigationController *secondNC = [[UINavigationController alloc]initWithRootViewController:secondVC];
//title
secondNC.tabBarItem.title = @"团购";
//image
secondNC.tabBarItem.image = [UIImage imageNamed:@"80-shopping-cart.png"];
//badgeValue
secondNC.tabBarItem.badgeValue = @"new";

//第三个
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
UINavigationController *thirdNC = [[UINavigationController alloc]initWithRootViewController:thirdVC];
//title
thirdNC.tabBarItem.title = @"发现";
//image
thirdNC.tabBarItem.image = [UIImage imageNamed:@"12-eye.png"];

//第四个
FourthViewController *fourthVC = [[FourthViewController alloc]init];
UINavigationController *fourthNC = [[UINavigationController alloc]initWithRootViewController:fourthVC];
fourthNC.tabBarItem.title = @"钱包";
fourthNC.tabBarItem.image = [UIImage imageNamed:@"35-shopping-bag.png"];

FifthViewController *fifthVC = [[FifthViewController alloc]init];
UINavigationController *fifthNC = [[UINavigationController alloc]initWithRootViewController:fifthVC];
fifthNC.tabBarItem.title = @"我的收藏";
fifthNC.tabBarItem.image = [UIImage imageNamed:@"28-star.png"];

SixthViewController *sixthVC = [[SixthViewController alloc]init];
UINavigationController *sixthNC = [[UINavigationController alloc]initWithRootViewController:sixthVC];
sixthNC.tabBarItem.title = @"工具箱";
sixthNC.tabBarItem.image = [UIImage imageNamed:@"36-toolbox.png"];

SeventhViewController *seventhVC = [[SeventhViewController alloc]init ];
UINavigationController *seventhNC = [[UINavigationController alloc]initWithRootViewController:seventhVC];
seventhNC.tabBarItem.title = @"设置";
seventhNC.tabBarItem.image = [UIImage imageNamed:@"20-gear2.png"];

//将七个导航视图控制器成为子控制器
self.viewControllers = @[firstNC,secondNC,thirdNC,fourthNC,fifthNC,sixthNC,seventhNC];

[firstVC release];
[firstNC release];
[secondVC release];
[secondNC release];
[thirdVC release];
[thirdNC release];
[fourthVC release];
[fourthNC release];
[fifthVC release];
[fifthNC release];
[sixthVC release];
[sixthNC release];
[seventhVC release];
[seventhNC release];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


<FirstViewController.h>

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end


<FirstViewController.m>

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

self.view.backgroundColor =[UIColor redColor];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


<SecondViewController.h>

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@end


<SecondViewController.m>

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor orangeColor];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
ThirdViewController,FourthViewController,FifthViewController,SixthViewController,SeventhViewController皆为空控制器。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: