iOS入門-24導航欄進階

概述

  • 演示一下在不同的設置情況下導航欄的狀態
  • 導航控制器控制視圖控制器切換

示例

  • 導航欄透明度屬性,導航欄底色和文字顏色;
  • 導航欄視圖控制器管理視圖控制器(頁面跳轉);

兩個示例代碼都放在一起了,第一個示例放在VCRoot.m視圖控制器中,仔細看註釋

第一個示例:導航欄透明度屬性,導航欄底色和文字顏色

看圖如下
VCRoot中設置self.navigationController.navigationBar.translucent = YES;
在這裏插入圖片描述

設置爲self.navigationController.navigationBar.translucent = NO;
在這裏插入圖片描述

self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
在這裏插入圖片描述

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
在這裏插入圖片描述

第二示例:導航欄視圖控制器管理視圖控制器(頁面跳轉)

示例就是演示頁面三連跳
默認情況下,非根視圖控制器左上角有一個返回鍵並帶名稱,名稱是上一個頁面的title
涉及到添加自己的UIWindow的知識參考前面的UIWindow

先看圖:
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

示例代碼

先看一下工程目錄結構
在這裏插入圖片描述

涉及到並做了修改的的文件寫在下面,沒用或者不做特殊處理的不貼出來了

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (retain,nonatomic) UIWindow* window;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "VCRoot.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    VCRoot* root = [VCRoot new];
    
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:root];
    
    self.window.rootViewController = nav;
    
    [self.window makeKeyAndVisible];
    return YES;
}


@end

VCRoot

#import "VCRoot.h"
#import "VCSecond.h"

@interface VCRoot ()

@end

@implementation VCRoot

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //添加一個紅色view
    UIView* v = [UIView new];
    v.frame = CGRectMake(40, 40, 200, 200);
    v.backgroundColor = [UIColor redColor];
    [self.view addSubview:v];
    
    self.navigationItem.title = @"第一頁";
    //導航欄透明度
    //YES:透明
    //NO:不透明
    self.navigationController.navigationBar.translucent = NO;
    
    //導航欄樣式,分白底黑字,黑底白字兩種
    //UIBarStyleDefault:白底黑字
    //UIBarStyleBlack:黑底白字
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    
    //導航欄右側添加一個按鈕,點擊跳轉到第二頁
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithTitle:@"去第二頁" style:UIBarButtonItemStyleDone target:self action:@selector(toSecond)];
    
    self.navigationItem.rightBarButtonItem = item;
    
    //視圖控制器本身設置爲藍色
    self.view.backgroundColor = [UIColor blueColor];
}

-(void) toSecond{
    //跳轉到第二個視圖控制器
    VCSecond* second = [VCSecond new];
    //跳轉方法
    //p1:視圖控制器對象
    //p2:是否帶動畫
    [self.navigationController pushViewController:second animated:YES];
}

/*
#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

VCSecond.m

#import "VCSecond.h"
#import "VCThird.h"

@interface VCSecond ()

@end

@implementation VCSecond

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"第二頁";
    
    UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"去第三頁" style:UIBarButtonItemStyleDone target:self action:@selector(toThird)];
    
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    self.view.backgroundColor = [UIColor redColor];
}

-(void) toThird{
    //跳到第三個視圖控制器
    VCThird* third = [VCThird new];
    
    [self.navigationController pushViewController:third animated:YES];
}

/*
#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

VCThird.m

#import "VCThird.h"

@interface VCThird ()

@end

@implementation VCThird

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"第三頁";
    
    self.view.backgroundColor = [UIColor greenColor];
}

/*
#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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章