iOS 第11課 標籤欄實作類別似qq底部的效果

來源:互聯網
上載者:User




0: 首先還是通過純的代碼來實現

0:刪除3個檔案ViewController.h,ViewController.m,Main.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







相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.