iOS 第11课 标签栏实现类似qq底部的效果

来源:互联网 发布:学而时之 不亦说乎注音 编辑:程序博客网 时间:2024/06/07 00:12




0: 首先还是通过纯的代码来实现

0:删除3个文件ViewController.hViewController.mMain.storyboard

1:修改点击左边的蓝色按钮,然后选择general-》developer info-》main interface ,将这个main interface 晴空


1: 然后生成四个视图控制器First Second Third Fourth,和一个类别

FirstViewController:

////  FirstViewController.m//  EllevenBiaoqianLan////  Created by 千雅爸爸 on 16/10/27.//  Copyright © 2016年 kodulf. All rights reserved.//#import "FirstViewController.h"#import "FourthViewController.h"@interface FirstViewController ()@end@implementation FirstViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    [self setTitle:@"First"];    [self.view setBackgroundColor:[UIColor yellowColor]];    NSLog(@"first: %s",__func__);//打印方法名字    //添加一个push按钮,然后点击之后让地步的导航栏也不显示    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"PUSH" style:UIBarButtonItemStylePlain target:self action:@selector(pushController)];}-(void)pushController{    FourthViewController *fourthVC = [[FourthViewController alloc]init];    //隐藏下面的标签栏    [fourthVC setHidesBottomBarWhenPushed:YES];        [self.navigationController pushViewController:fourthVC animated:YES];}- (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

SecondViewController

////  SecondViewController.m//  EllevenBiaoqianLan////  Created by 千雅爸爸 on 16/10/27.//  Copyright © 2016年 kodulf. All rights reserved.//#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    [self setTitle:@"Second"];    [self.view setBackgroundColor:[UIColor redColor]];    NSLog(@"second: %s",__func__);//打印方法名字}- (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


ThirdViewController

////  ThirdViewController.m//  EllevenBiaoqianLan////  Created by 千雅爸爸 on 16/10/27.//  Copyright © 2016年 kodulf. All rights reserved.//#import "ThirdViewController.h"@interface ThirdViewController ()@end@implementation ThirdViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    [self setTitle:@"Third"];    [self.view setBackgroundColor:[UIColor blueColor]];    NSLog(@"third: %s",__func__);//打印方法名字}- (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


FourthViewController

////  FourthViewController.m//  EllevenBiaoqianLan////  Created by 千雅爸爸 on 16/10/27.//  Copyright © 2016年 kodulf. All rights reserved.//#import "FourthViewController.h"@interface FourthViewController ()@end@implementation FourthViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    [self setTitle:@"Fourth"];    [self.view setBackgroundColor:[UIColor brownColor]];}- (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


UIImage+OriginalImage

////  UIImage+OriginalImage.h//  EllevenBiaoqianLan////  Created by 千雅爸爸 on 16/10/27.//  Copyright © 2016年 kodulf. All rights reserved.//#import <UIKit/UIKit.h>//默认上面的图片都是做过了毛玻璃的效果的,如果不想要毛玻璃的效果,那么久直接@interface UIImage (OriginalImage)+(UIImage *) originalImageName:(NSString *)name;@end

////  UIImage+OriginalImage.m//  EllevenBiaoqianLan////  Created by 千雅爸爸 on 16/10/27.//  Copyright © 2016年 kodulf. All rights reserved.//#import "UIImage+OriginalImage.h"@implementation UIImage (OriginalImage)//默认上面的图片都是做过了毛玻璃的效果的,如果不想要毛玻璃的效果,那么久直接+(UIImage *) originalImageName:(NSString *)name{    return [[UIImage imageNamed:name]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];}@end


2: 然后修改app delegate.m文件

////  AppDelegate.m//  EllevenBiaoqianLan////  Created by 千雅爸爸 on 16/10/27.//  Copyright © 2016年 kodulf. All rights reserved.//#import "AppDelegate.h"#import "FirstViewController.h"#import "SecondViewController.h"#import "ThirdViewController.h"//默认上面的图片都是做过了毛玻璃的效果的,如果不想要毛玻璃的效果,那么久直接//导入我们创建的分类#import "UIImage+OriginalImage.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    //导航栏控制器是一种栈的形式,    //标签栏控制器,就有点像是android 里面的ViewPager和RadioGroup结合的形式,但是又是不同的,标签栏控制器里面的组件,例如有导航栏控制器,有其它的,会都显示只不过是有的在前面,有的在后面,相当于是并列的显示,请查看说明的图    //我们一板是将导航栏控制器设置为标签栏控制器的子控制器    //首先创建四个控制器的类,然后是一个UIImage+OriginalImage的分类            [self setWindow:[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]];    [self.window makeKeyAndVisible];    UITabBarController *tabBarController = [[UITabBarController alloc] init];    [tabBarController.view setBackgroundColor:[UIColor whiteColor]     ];    [self.window setRootViewController:tabBarController];        FirstViewController *firstVC=[[FirstViewController alloc]init];    //把first先放在navigationcontroler上面,然后再将navigationcontroller添加到    firstVC.tabBarItem.title = @"first";//地下的说明的文字    firstVC.tabBarItem.image = [UIImage imageNamed:@"64"];//默认的图片    //firstVC.tabBarItem.selectedImage =[UIImage imageNamed:@"cat"];//选中后的效果    //默认上面的图片都是做过了毛玻璃的效果的,如果不想要毛玻璃的效果,那么久直接    //这里的选中和补选中的显示就和android 中的selector很像    firstVC.tabBarItem.selectedImage =[UIImage originalImageName:@"cat"];//选中后的效果    UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:firstVC];        //直接复制粘贴上面的first 生成second third    SecondViewController *secondVC = [[SecondViewController alloc]init];    secondVC.tabBarItem.title =@"second";    secondVC.tabBarItem.image = [UIImage imageNamed:@"64"];    secondVC.tabBarItem.selectedImage=[UIImage originalImageName:@"cat"];    UINavigationController *secondNavigationController = [[UINavigationController alloc]initWithRootViewController:secondVC];        ThirdViewController *thirdVC = [[ThirdViewController alloc]init];    thirdVC.tabBarItem.title = @"third";    thirdVC.tabBarItem.image =[UIImage imageNamed:@"64"];    thirdVC.tabBarItem.selectedImage = [UIImage originalImageName:@"cat"];    UINavigationController *thirdNavigationController = [[UINavigationController alloc]initWithRootViewController:thirdVC];        //还可以给tabbar 设置背景图片和颜色    [tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"green2"]];    //[tabBarController.tabBar setBackgroundColor:[UIColor greenColor]];        //统一设置字体的大小    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} forState:UIControlStateNormal];    [tabBarController setViewControllers:@[navigationController,secondNavigationController,thirdNavigationController]];    //默认的tabbar选择的是第一个被加入到数组中的    //懒加载的方式,用到哪个就去加在哪个,而且一旦加载了不会轻易的被回收    //但是如果出现内存警告了,那么系统会优先处理掉那些当前用不到的界面        // Override point for customization after application launch.    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







0 0