您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

UINavigationController 重點整理:堆疊、按鈕與導航列樣式

編輯:iOS 相關資訊

Navigation Controller 的堆疊概念

UINavigationController 以堆疊管理畫面。push 代表入堆疊,pop 代表出堆疊。最先加入的是根視圖控制器,最後加入的在最上層。系統內建的返回按鈕,本質上就是一次 pop 操作。

常用方法

初始化時可用 initWithRootViewController: 指定根控制器;後續可用 pushViewController:animated: 推入新畫面,或用 popViewControllerAnimated: 返回上一頁。若需要自訂返回行為,也可在 push 時設定 backBarButtonItem。

自訂左右按鈕

navigationItem 的 leftBarButtonItem 與 rightBarButtonItem 可放 UIBarButtonItem。若需要多個按鈕,可改用 leftBarButtonItems 或 rightBarButtonItems。

self.navigationItem.leftBarButtonItem = self.iCityBarButton;
self.navigationItem.rightBarButtonItem = self.iCityBarButton;

返回按鈕文字

若想修改下一頁的返回按鈕文字,可在目前頁設定 backBarButtonItem,而不是直接改下一頁的 title。這種做法比較直覺,也較不會影響其他畫面。

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:nil];

更換導航列背景與文字顏色

在 iOS 5 之後,可用 setBackgroundImage:forBarMetrics: 直接換背景;也可用 tintColor 調整按鈕與文字顏色。若需要更高度自訂,才需要繼承 UINavigationBar 並覆寫 drawRect:。

[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:1.0 green:0.38 blue:0.63 alpha:1.0]];
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved