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

IOS 两种控制器的使用,纯代码UITabBarController 与 UINavigationController

时间:2017-03-15 10:34:27      阅读:688      评论:0      收藏:0      [点我收藏+]

标签:mat   events   效果图   event   point   第三方   app   div   nsarray   

先说简单的吧,UINavigationController代码创建非常简单,仅需一行代码

//NewsViewController是你创建的一个View
NewsViewController *newsPage = [[NewsViewController alloc]init];
UINavigationController *newsNav = [[UINavigationController alloc] initWithRootViewController:newsPage];

然后是重头戏,UITabBarController,我刚开始接触这个控制器是在stroryboard里面直接拖,非常简单方便,但是后来在实际项目里面这种方式是不适合的,需要用代码来创建,我的需求是做一个引导页,然后点击引导页上面的一个按钮跳转到UITabBarController页面.遍查百度好像都是在AppDelegate.h这个里面写,我不喜欢在这个里面写,,,,,,终于找到了一种方式,非常简单,一个第三方库RDVTabBarController,废话不多说,上代码!!!

AppDelegate.h

//简单的把viewController作为程序第一响应页面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // 修改导航颜色 [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:100 green:80 blue:50 alpha:1]]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; ViewController *vc = [[ViewController alloc] init]; self.window.rootViewController = vc; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }

//继承RDVTabBarController

ViewController.h

#import "RDVTabBarController.h"

@interface ViewController : RDVTabBarController


@end

 

ViewController.m


#import "ViewController.h"
#import "RDVTabBarItem.h"
#import "RDVTabBar.h"
#import "RDVTabBarController.h"

@interface ViewController ()

@end

@implementation ViewController{
    // 初次登陆引导页
    UIScrollView *_firstLoginSV;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSInteger inter = [user integerForKey:@"isFirstLogin"];
    if (!inter) {
        self.navigationController.navigationBarHidden = YES;
        self.rdv_tabBarController.tabBar.hidden = YES;
        [self createFirstEnterImage];
        [user setInteger:1 forKey:@"isFirstLogin"];
    }else{
        [self setUpViewControllers];
    }
    
    
    
    
}

// 初次登录APP引导页
- (void)createFirstEnterImage{
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    
    _firstLoginSV = [[UIScrollView alloc] initWithFrame:self.view.frame];
    _firstLoginSV.contentSize = CGSizeMake(3 * SCREEN_WIDTH, SCREEN_HEIGHT);
    _firstLoginSV.backgroundColor = [UIColor yellowColor];
    _firstLoginSV.pagingEnabled = YES;
    _firstLoginSV.bounces = NO;
    [self.view addSubview:_firstLoginSV];
    
    NSArray *imageArray = @[@"back3",@"back4", @"back5"];
    for (int i = 0; i < imageArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        imageView.image = [UIImage imageNamed:imageArray[i]];
        [_firstLoginSV addSubview:imageView];
    }
    
    // 创建进入APP Button
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(2*SCREEN_WIDTH+(SCREEN_WIDTH - 150)/2, (SCREEN_HEIGHT - 100)/2, 150, 100)];
    [btn setTitle:@"开始旅程" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(enterBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_firstLoginSV addSubview:btn];

}

- (void)enterBtnClick {
    [_firstLoginSV removeFromSuperview];
    [self setUpViewControllers];
    self.selectedIndex = 0;
}

//创建uitabar
-(void)setUpViewControllers{
    
    NewsViewController *newsPage = [[NewsViewController alloc]init];
    UINavigationController *newsNav = [[UINavigationController alloc] initWithRootViewController:newsPage];
    
    VideoViewController *preferentialPage = [[VideoViewController alloc] init];
    UINavigationController *preferentialNav = [[UINavigationController alloc] initWithRootViewController:preferentialPage];
    HappyViewController *happyPage = [[HappyViewController alloc] init];
    UINavigationController *happyNav = [[UINavigationController alloc] initWithRootViewController:happyPage];
    NSArray *items = @[newsNav, preferentialNav, happyNav];
    self.viewControllers = items;
    [self customizeTabBarForController:self];
    
}

-(void)customizeTabBarForController:(RDVTabBarController *)tabBarController{
    NSArray *tabBarItemTitle = @[@"每日新闻", @"不得其解", @"开心段子"];
    NSArray *tabBarItemImages = @[@"news", @"shouye", @"duanzi"];
    NSDictionary *selectedTitleAttributes = nil;
    NSDictionary *unselectedTitleAttributes = nil;
    
    self.rdv_tabBarController.tabBar.tintColor = [UIColor orangeColor];
    
   
    NSInteger index = 0;
    for (RDVTabBarItem *item in [[tabBarController tabBar] items]) {
        // 设置 title
        item.title = tabBarItemTitle[index];
        // 获取未选中图片的名字
        NSString *normalImageName = [NSString stringWithFormat:@"%@_normal.png", tabBarItemImages[index]];
        // 获取选中状态图片的名字
        NSString *selectedImageName = [NSString stringWithFormat:@"%@_selected.png", tabBarItemImages[index]];
        // 选中状态 image
        UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
        // 未选中状态 image
        UIImage *normalImage = [UIImage imageNamed:normalImageName];
        // 设置 item 选中和未选中的图标
        [item setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:normalImage];
        // 设置选中状态 title 的字体大小和颜色
        item.selectedTitleAttributes = selectedTitleAttributes;
        // 设置未选中状态 title 的字体大小和颜色
        item.unselectedTitleAttributes = unselectedTitleAttributes;
        
        index++;
    }

}


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


@end

效果图

技术分享   技术分享

技术分享

 

IOS 两种控制器的使用,纯代码UITabBarController 与 UINavigationController

标签:mat   events   效果图   event   point   第三方   app   div   nsarray   

原文地址:http://www.cnblogs.com/qiyiyifan/p/6552813.html

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