一、導覽欄 UINavigationBar
1、導覽欄的使用
在 iOS 開發中,我們通常會使用導覽控制器,其中封裝了一個 UINavigationBar。實際上,我們也可以在不使用導覽控制器的前提下單獨使用導覽欄。在 UINavigationBar 中,有許多可以客製的屬性,用起來十分方便。
2、UINavigationBar 的建立與風格類型
導覽欄繼承自 UIView,所以我們可以像建立普通視圖那樣建立導覽欄。例如建立一個高度為 80 的導覽欄,放在 ViewController 的頂部,程式碼如下:
UINavigationBar *bar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];
[self.view addSubview:bar];效果如下:

我們也可以設定導覽欄的風格屬性。自 iOS 6 之後,UINavigationBar 預設為半透明的樣式,從上圖也能看出,白色導覽欄下方透出些許背景的紅色。導覽欄的風格可透過下面的屬性設定:
@property(nonatomic,assign) UIBarStyle barStyle;UIBarStyle 是一個列舉,其中大部分樣式都已棄用,有效果的只有以下兩個:
typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault = 0,//預設
UIBarStyleBlack = 1,//黑色
}預設風格就是我們上面看到的白色風格,黑色風格效果如下:

3、導覽欄常用屬性與方法
從上面可以看到,iOS 6 之後導覽欄預設都是半透明的,我們可以透過下面的布林值來設定,設為 NO 則導覽欄不透明,預設為 YES:
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent;下面一些屬性用於設定 NavigationBar 及其上面 item 的顏色:
@property(null_resettable, nonatomic,strong) UIColor *tintColor;tintColor 會影響導覽欄左側 pop 按鈕的圖案顏色與字型顏色,系統預設如下:

@property(nullable, nonatomic,strong) UIColor *barTintColor;barTintColor 用於設定導覽欄的背景色,這個屬性被設定後,半透明效果將失效:

- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (nullable UIImage *)backgroundImageForBarMetrics:(UIBarMetrics)barMetrics;上面兩個方法用於設定與取得導覽欄的背景圖案。這裡要留意,預設背景圖案不做縮放處理,所以使用的圖片尺寸要和導覽欄尺寸相符。其中還有一個 UIBarMetrics 參數,用來設定設備狀態:
typedef NS_ENUM(NSInteger, UIBarMetrics) {
UIBarMetricsDefault,//正常豎屏狀態
UIBarMetricsCompact,//橫屏狀態
};
//設定導覽欄的陰影圖片
@property(nullable, nonatomic,strong) UIImage *shadowImage;
//設定導覽欄的標題字型屬性
@property(nullable,nonatomic,copy) NSDictionary<NSString *,id> *titleTextAttributes;標題字型屬性會影響導覽欄中間的標題,如下:
bar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};

我們也可以透過下面的屬性設定導覽欄標題的垂直位置偏移:
- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics;
- (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics;還有一個細節:導覽欄左側 pop 按鈕的圖案預設是一個箭頭,我們可以用下面的方法修改:
@property(nullable,nonatomic,strong) UIImage *backIndicatorImage;
@property(nullable,nonatomic,strong) UIImage *backIndicatorTransitionMaskImage;
4、導覽欄中 item 的 push 與 pop 操作
UINavigationBar 上面不只是簡單地顯示標題,它也對標題進行了堆疊管理。每一個標題抽象出的物件在 iOS 系統中是 UINavigationItem,我們可以透過 push 與 pop 操作管理 item 群組。
//向堆疊中加入一個 item,上一個 item 會被推向導覽欄的左側,變為 pop 按鈕,會有一個動畫效果
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
//pop 一個 item
- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
//當前 push 到最上層的 item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
//僅次於最上層的 item,一般是被推向導覽欄左側的 item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
//取得堆疊中所有 item 的陣列
@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
//設定一組 item
- (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated;
5、UINavigationBarDelegate
在 UINavigationBar 中,還有如下一個屬性:
@property(nullable,nonatomic,weak) id<UINavigationBarDelegate> delegate;透過代理,我們可以監控導覽欄的一些 push 與 pop 操作:
//item 將要 push 時呼叫,回傳 NO 則不能 push
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item;
//item 已經 push 後呼叫
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item;
//item 將要 pop 時呼叫,回傳 NO 則不能 pop
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;
//item 已經 pop 後呼叫
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;
二、工具欄 UIToolBar
導覽欄一般出現在視圖的頂部,與之相對,工具欄一般出現在視圖的底部,上面可以填入一些按鈕,提供給使用者一些操作。建立一個工具欄如下:
self.view.backgroundColor = [UIColor grayColor];
UIToolbar * tool = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-40, 320, 40)];
[self.view addSubview:tool];

下面是 UIToolBar 中的一些方法,其中大部分在 UINavigationBar 中都有涉及,這裡只做簡單介紹:
//工具欄的風格,和導覽欄類似,有黑白兩種
@property(nonatomic) UIBarStyle barStyle;
//設定工具欄上按鈕陣列
@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *items;
//設定工具欄是否透明
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent;
//設定工具欄按鈕
- (void)setItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated;
//設定 item 風格顏色
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
//設定工具欄背景色
@property(nullable, nonatomic,strong) UIColor *barTintColor;
//設定工具欄背景和陰影圖案
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;
- (nullable UIImage *)backgroundImageForToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;
- (void)setShadowImage:(nullable UIImage *)shadowImage forToolbarPosition:(UIBarPosition)topOrBottom;
- (nullable UIImage *)shadowImageForToolbarPosition:(UIBarPosition)topOrBottom;