首页 > 代码库 > IOS 两种控制器的使用,纯代码UITabBarController 与 UINavigationController
IOS 两种控制器的使用,纯代码UITabBarController 与 UINavigationController
先说简单的吧,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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。