iOS開發-UI (六)Navigation,ios-ui

來源:互聯網
上載者:User

iOS開發-UI (六)Navigation,ios-ui

知識點:

1.UINavigationController

2.UINavigationItem

3.UINavigationBar

4.UINavigationController視圖切換

 

========================

UINavigationController

    1.什麼是導航控制器

作用:管理檢視控制器

 

    2.UINavigationController對象建立

      1)初始化方式

- (id)initWithRootViewController:(UIViewController *)rootViewController

 

      2)UINavigationController組成:

(1)navigationBar(高度44)

(2)customContent  — 有自訂的ViewController提供

(3)navigationToolbar(高度44)

 

 

    3.通過UINavigationController對象切換視圖

      1)將視圖控制器壓入導航控制器的棧容器中

- (void)pushViewController:(UIViewController *)viewController 

  animated:(BOOL)animated

      2)將視圖控制器從導航控制器中彈出

- (UIViewController *)popViewControllerAnimated:(BOOL)animated

  

========================

UINavigationItem

 

UINavigationItem包含了:

(1)backBarButtonItem(由上一級ctl的屬性決定)

(2)title/titleView(當前ctl)

(3)rightBarButtonItem(當前ctl)

(4)leftBarButtonItem(當前ctl)

 

 

    1.UINavigationItem和UIViewController關係

navigationItem是UIViewController的一個屬性

這個屬性是為UINavigationController服務的

 

 

    2.建立UIBarButtonItem

  1)建立系統內建的UIBarButtonSystemItem

      - (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem 

      target:(id)target 

  action:(SEL)action;

 

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(btnAction)];

      2)文字UIBarButtonItem的建立方式

- (id)initWithTitle:(NSString *)title

      style:(UIBarButtonItemStyle)style

    target:(id)target

    action:(SEL)action

 

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem new];

 

      3)圖片UIBarButtonItem的建立方式

- (id)initWithImage:(UIImage *)image 

      style:(UIBarButtonItemStyle)style

    target:(id)target

    action:(SEL)action

 

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"refresh_30"] style:UIBarButtonItemStylePlain target:nil action:nil];

 

      3)如何將UIBarButtonItem加入到導航的左邊和右邊

@property (nonatomic, retain) UIBarButtonItem *leftBarButtonItem

@property (nonatomic, retain) UIBarButtonItem *rightBarButtonItem

@property (nonatomic, retain) UIBarButtonItem *backBarButtonItem

 

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:b];

 

    3.定製UIBarButtonItem

 

    4.定製導航中間的titleView

self.navigationItem.title = @"控制器2";

 

    5.定製backBarButtonItem

      注意:設定當前控制器的backBarButtonItem需要在下一個控制器中才能顯示

 

 

========================

UINavigationBar

    1.如何往UINavigationBar貼圖

      設定背景圖片

      - (void)setBackgroundImage:(UIImage *)backgroundImage

    forBarMetrics:(UIBarMetrics)barMetrics

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"1.png"] forBarMetrics:UIBarMetricsDefault];

 

    2.如何設定UINavigationBar設定顏色

      @property (nonatomic, retain) UIColor *tintColor

      @property (nonatomic, retain) UIColor *barTintColor

 

    3.如何設定透明顏色

      1)設定UINavigationBar的樣式

      @property (nonatomic, assign) UIBarStyle barStyle

   self.navigationController.navigationBar.barStyle= UIBarStyleBlack;

 

      2)是否透明

@property (nonatomic, assign, getter=isTranslucent) BOOL translucent

 

//擷取管理當前視圖控制器的導航控制器(如果這個視圖控制器沒有受到導航控制器管理,此方法會返回null 指標)

    //設定導覽列為不透明,座標點會自動下移64個單位

    self.navigationController.navigationBar.translucent = NO;

 

      3)改變導覽列的顏色

@property(nonatomic,retain) UIColor *barTintColor 

 

//設定背景色

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];

 

    4.如何隱藏UINavigationBar

      1)不帶動畫隱藏

      @property (nonatomic, getter=isNavigationBarHidden) BOOL  navigationBarHidden

 

//顯示

    self.navigationController.navigationBarHidden = NO;

 

      2)帶動畫隱藏

- (void) setNavigationBarHidden:(BOOL)navigationBarHidden 

      animated:(BOOL)animated

      [self.navigationController setNavigationBarHidden:YES animated:YES];

 

========================

UINavigationController視圖切換

    1.擷取導航控制器中的視圖數組

@property (nonatomic, copy) NSArray *viewControllers

 

    2.將視圖控制器壓入導航控制器的棧容器中

- (void)pushViewController:(UIViewController *)viewController 

  animated:(BOOL)animated

 

//視圖控制器入棧

    [self.navigationController pushViewController:ctlA animated:YES];

 

    3.將視圖控制器從導航控制器中彈出

- (UIViewController *)popViewControllerAnimated:(BOOL)animated

 

    4.切換至指定的視圖控制器(該控制器必須在當前置航控制器中的棧中)

- (NSArray *)popToViewController:(UIViewController *)viewController

                                animated:(BOOL)animated

    5.回到根視圖控制器

     - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.