首页 > 其他 > 详细

分栏控制器和导航栏目tabBarItem,UINavigationController

时间:2015-09-25 23:07:26      阅读:484      评论:0      收藏:0      [点我收藏+]

//
//  AppDelegate.m
//  TabBarControllerDemo
//
//  Created by qianfeng on 15/9/22.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"
#import "ViewController5.h"
#import "ViewController6.h"
#import "ViewController7.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

#pragma mark    ------------------------创建customTabBar
- (void)customTabBar
{
    //取出分栏控制器
    UITabBarController *vc = (id)self.window.rootViewController;
    
    //bar样式
    vc.tabBar.barStyle = UIBarStyleBlack;
    
    //半透明度
    vc.tabBar.translucent = NO;
    
    //tint color是设置你选中的那个tabBar的颜色,默认是蓝色
    vc.tabBar.tintColor = [UIColor greenColor];
    
    //bar tint color
    vc.tabBar.barTintColor = [UIColor whiteColor];
    
}

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

    #pragma mark    ------------------------VC1
    
    ViewController1 *vc1 = [[ViewController1 alloc] init];
    
    vc1.title = @"微信";
    
    //vc1.view.backgroundColor = [UIColor whiteColor];
    
    vc1.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:1];
    
    vc1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"home" image:[UIImage imageNamed:@"tabbar_mainframeHL@2x"] tag:2];
    
    //创建导航控制器
    UINavigationController *nvc1 = [[UINavigationController alloc]initWithRootViewController:vc1];

    nvc1.navigationBar.barStyle = UIBarStyleBlack;
    
    //nvc1.navigationBar.barTintColor = [UIColor redColor];
    
    nvc1.navigationBar.tintColor = [UIColor yellowColor];//字体颜色
    
   //nvc1.navigationBar.translucent = NO;
    
    #pragma mark    ------------------------VC2
    
    ViewController2 *vc2 = [[ViewController2 alloc] init];
    
    vc2.title = @"通讯录";
    
    vc2.view.backgroundColor = [UIColor whiteColor];
    
    vc2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"通讯录" image:[UIImage imageNamed:@"tabbar_contactsHL@2x"] tag:2];
    
    //创建导航控制器
    UINavigationController *nvc2 = [[UINavigationController alloc]initWithRootViewController:vc2];
    
    nvc2.navigationBar.tintColor = [UIColor whiteColor];
    
    nvc2.navigationBar.barStyle = UIBarStyleBlack;//背景是黑色
    
    #pragma mark    ------------------------VC3
    
    ViewController3 *vc3 = [[ViewController3 alloc] init];
    
    vc3.title = @"发现";
    
    vc3.view.backgroundColor = [UIColor whiteColor];
    
    vc3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"tabbar_discover@2x"] selectedImage:[UIImage imageNamed:@"tabbar_discoverHL@2x"]];
    
    vc3.tabBarItem.tag = 3;
    
    
    //创建导航控制器
    UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:vc3];
    
    nvc.navigationBar.barStyle = UIBarStyleBlack;//背景是黑色
    
    #pragma mark    ------------------------VC4
    
    ViewController4 *vc4 = [[ViewController4 alloc] init];
    
    vc4.title = @"我";
    
    vc4.view.backgroundColor = [UIColor whiteColor];
    
    vc4.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"tabbar_me@2x"] tag:4];
    
    //创建导航控制器
    UINavigationController *nvc4 = [[UINavigationController alloc]initWithRootViewController:vc4];
    
    nvc4.navigationBar.barStyle = UIBarStyleBlack;//背景是黑色
    
    #pragma mark    ------------------------VC5
    
    ViewController5 *vc5 = [[ViewController5 alloc] init];
    
    vc5.title = @"页五";
    
    vc5.view.backgroundColor = [UIColor cyanColor];
    
    vc5.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:5];
    
    #pragma mark    ------------------------VC6
    
    ViewController6 *vc6 = [[ViewController6 alloc] init];
    
    vc6.title = @"页六";
    
    vc6.view.backgroundColor = [UIColor orangeColor];
    
    vc6.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:6];
    
    #pragma mark    ------------------------VC7
    
    ViewController7 *vc7 = [[ViewController7 alloc]init];
    
    vc7.title = @"页七";
    
    vc7.view.backgroundColor = [UIColor blueColor];
    vc7.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemRecents tag:7];
    
    #pragma mark    ------------------------创建分栏目控制器等
    
    //分栏控制器 最多能显示五个
    UITabBarController *tvc = [[UITabBarController alloc]init];
    
    tvc.viewControllers = @[nvc1,nvc2,nvc,nvc4];//,vc5,vc6,vc7
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds] ];
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    
    self.window.rootViewController = tvc;
    
    [self customTabBar];         //创建分栏控制器
    
    //设定代理
    tvc.delegate = self;
    
    //设置选择控制器
    tvc.selectedIndex = 0;
    
//    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
//    
//    NSString * value = [ud objectForKey:@"title"];
//    
//    for (UIViewController *vc in tvc.viewControllers) {
//        if ([vc.title isEqualToString:value]) {
//            tvc.selectedViewController = vc;
//        }
//    }
    
    //NSLog(@"%@",NSHomeDirectory());
    
    [self.window makeKeyAndVisible];
    
    
    return YES;
}



#pragma mark    ------------------------ 实现分栏控制器代理方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewControll{
    
    //是否可以选择这个控制器,yes 允许选择,no 不允许
//    if (viewControll.tabBarItem.tag == 2) {
//        return NO;
//    }
//    
    return YES;
    
}

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
{
    //将要开始编辑的时候,会调用这个方法
    for (UIViewController *vc in viewControllers) {
        
        NSLog(@"tag = %ld",vc.tabBarItem.tag);
        
    }
    
}

- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
    //编辑结束的时候会调用这个方法
    for (UIViewController *vc in viewControllers) {
        
        NSLog(@"changed tag = %ld",vc.tabBarItem.tag);
        
    }
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    //选择视图控制器之后,调用这个方法
    NSLog(@"vc tag = %ld",viewController.tabBarItem.tag);
    
    //是一个单例
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    
    [ud setObject:viewController.title forKey:@"title"];
    
    //写入文件
    [ud synchronize];
}


@end

 

技术分享

 

 

技术分享技术分享

分栏控制器和导航栏目tabBarItem,UINavigationController

原文:http://www.cnblogs.com/wanghengheng/p/4839574.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!