UINavigationController基本使用

寫了很長的NavigationController介紹,結果被cnblog吞了,沒存檔,算了,簡單粗暴,直接上如何使用。數組

  1.建立3個Controller,繼承自UIViewControllerapp

AppDelegate.h中ide

//
//  AppDelegate.m
//  UINavigationController
//
//  Created by lcd on 15/9/11.
//  Copyright (c) 2015年 lcd. All rights reserved.
//

#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

FirstViewController *firstViewController = [[FirstViewController alloc] init];字體

 
 

    firstViewController.view.backgroundColor = [UIColor redColor];atom

    SecondViewController *secondViewController = [[SecondViewController alloc] init];spa

    secondViewController.view.backgroundColor = [UIColor whiteColor];3d

    ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];code

   thirdViewController.view.backgroundColor = [UIColor cyanColor];xml

//建立導航控制器
    UINavigationController *navigation = [[UINavigationController alloc] init];
    
    return YES;
}

2.使用NavigationController有多種方式blog

//第一種:將須要NavigationController 管理的視圖添加進NavigationController 的子視圖數組中
    UINavigationController *navigation = [[UINavigationController alloc] init];
    navigation.viewControllers = @[firstViewController,secondViewController,thirdViewController];
//第二種:逐個添加子控制器
    [navigation addChildViewController:firstViewController];
    [navigation addChildViewController:secondViewController];
    [navigation addChildViewController:thirdViewController];
//第三種:初始化NavigationController時添加自控制器
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:firstViewController];

3.添加子控制器後,將navigation設置爲窗口的根控制器

self.window.rootViewController = navigation;

這樣運行,顯示的是第三個ViewController,這是由於,NavigationController是以棧的方式管理子視圖,如圖:

先添加的視圖被放在了棧底,顯示在咱們面前的是棧頂視圖。點擊back會把棧頂視圖彈出,即爲出棧(pop)操做。

NavigationController以 push(入棧)和pop(出棧)管理子視圖。

4. NavigationController 結構

image: ../Art/nav_controllers_objects.jpg

4.1導航欄

被NavigationController管理的視圖公用一個導航欄,導航欄顯示內容由棧頂控制器決定

ThirdViewController.m中

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我是第三個控制器";
}

左上角返回鍵文字(Back) 由上一個控制器控制

在SecondViewController.m中

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回第二個界面" style:nil target:nil action:nil];
}

運行結果以下圖:

4.2 在導航欄添加按鈕

UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];
    
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil];
    
self.navigationItem.rightBarButtonItems = @[button1, button2];

修改右按鈕格式

self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];
//左按鈕同理

隱藏back按鈕

[self.navigationItem setHidesBackButton:YES];

 

 

4.3 設置導航條樣式

[self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];

//樣式枚舉值以下:

typedef NS_ENUM(NSInteger, UIBarStyle) {

    UIBarStyleDefault          = 0,

    UIBarStyleBlack            = 1,

    UIBarStyleBlackOpaque      = 1, // Deprecated. Use UIBarStyleBlack

    UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES

};

設置導航欄圖片

[self.navigationController.navigationBar
setBackgroundImage:[UIImage imageNamed:@"圖片名稱"] forBarMetrics:UIBarMetricsDefault];

 

4.4 其餘屬性

//設置導航條顏色
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    //關閉導航條毛玻璃效果
//    self.navigationController.navigationBar.translucent = NO;
    //隱藏導航條
//    self.navigationController.navigationBarHidden = YES;
    //設置導航條內容的顏色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //導航條背景圖片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"pic"] forBarMetrics:UIBarMetricsDefault];

titleTextAttributes

//這是UINavigationBar的一個屬性,經過它你能夠設置title部分的字體,這個屬性定義以下
@property(nonatomic,copy) NSDictionary *titleTextAttributes __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;  
//設置title的字體顏色爲黃色
NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIColor yellowColor] forKey:UITextAttributeTextColor];  
firstViewController.navigationController.navigationBar.titleTextAttributes = dict;

 

5.界面跳轉

從FirstViewController跳轉到SceondViewController

在FirstViewController.m中,添加button,並在button事件中添加以下代碼:

 SecondViewController *secondViewController = [[SecondViewController alloc] init];
 [self.navigationController pushViewController:secondViewController animated:YES];

從SceondViewController返回FirstViewController:

[self.navigationController popViewControllerAnimated:YES];

注意:從後一個界面跳轉回前一個界面,必定要用popViewController,千萬不要alloc一個FirstViewController,push到FirstViewController

[self.navigationController popToRootViewControllerAnimated:YES];//這個方法能夠跳轉回第一個界面
相關文章
相關標籤/搜索