本文介紹 UINavigationController
導航視圖控制器相關的常用方法。
1. 創建 UINavigationController
并設置為應用窗口的根視圖控制器
初始化 UINavigationController
對象時,需要傳入 UIVIewController
實例對象的參數作為它的根視圖控制器。再將 UINavigationController
對象設置為 UIWindow
應用窗口對象的根視圖控制器。
在 Xcode 11 之前新創建的項目,示例代碼如下:
/*
* 視圖控制器層級結構:
* UIWindow -> UINavigationController -> MainTableViewController
*
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 設置和初始化應用窗口的根視圖控制器
// 初始化應用窗口
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 創建主列表視圖控制器對象
MainTableViewController *mainTableViewController = [[MainTableViewController alloc] initWithStyle:UITableViewStylePlain];
// ??設置窗口根視圖控制器:UINavigationController
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:mainTableViewController];
// 設置窗口背景色
self.window.backgroundColor = [UIColor whiteColor];
// 使窗口可見
[self.window makeKeyAndVisible];
// ??可選,全局設置窗口導航欄顏色、字體
[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
return YES;
}
在 Xcode 11 之后創建的項目,因為 Apple 引入了 UIScene
特性,示例代碼如下:
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (@available(iOS 13.0, *)) {
} else {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *con = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:con];
[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
}
return YES;
}
// SceneDelegate.m
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// 使用此方法可以有選擇地配置 UIWindow 窗口并將其附加到提供的 UIWindowScene 場景中。
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// 如果使用 storyboard,window 屬性將會自動初始化并附加到場景中
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// 該委托并不意味著連接場景或會話是新的(請參見 `application:configurationForConnectingSceneSession`)
if (@available(iOS 13.0, *)) {
UIWindowScene *windowScene = (UIWindowScene *)scene;
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window setWindowScene:windowScene];
ViewController *con = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:con];
[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
}
}
2. 隱藏導航欄、工具欄
隱藏當前視圖控制器頂部的導航欄:
[self.navigationController setNavigationBarHidden:YES];
隱藏當前視圖控制器底部的工具欄:
[self.navigationController setToolbarHidden:YES];
使用場景
每當進入詳情頁面時,隱藏頁面頂部的導航欄和頁面底部的工具欄,推出該詳情頁時(即返回到上一個頁面),再顯示回頁面頂部的導航欄和工具欄。
#pragma mark - Lifecycle
// 每當進入此頁面時,隱藏頁面頂部的導航欄和頁面底部的工具欄
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
[self.navigationController setToolbarHidden:YES];
}
// 每當退出此頁面時,不再隱藏頁面頂部的導航欄和頁面底部的工具欄
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO];
[self.navigationController setToolbarHidden:NO];
}
3. 視圖入棧和出棧
3.1 推入下一個視圖控制器
- (void)pushToNextViewController {
// 實例化下一個視圖控制器
ViewController *secondViewController = [[ViewController alloc] initWithNibName:NSStringFromClass([ViewController class]) bundle:nil];
// 將該視圖控制器推入到導航視圖控制器中,相當于入棧操作
[self.navigationController pushViewController:secondViewController animated:YES];
}
3.2 返回上一個視圖控制器
- (void)popToLastViewController {
// 當前視圖控制器,將從導航視圖控制器堆棧中移除,并返回至上一個視圖控制器,相當于出棧操作
[self.navigationController popViewControllerAnimated:YES];
}
3.3 根據索引返回到指定的視圖控制器
- (void)gotoIndexViewController {
// 根據導航視圖控制器中的全局序號,查找堆棧中指定序號的視圖控制器
UIViewController *viewController = [[self.navigationController viewControllers] objectAtIndex:2];
// 然后跳轉至該視圖控制器
[self.navigationController popToViewController:viewController animated:YES];
}
3.4 返回到指定的視圖控制器
通過 for-in
循環遍歷 UINavigationController
的 viewControllers
數組,找到需要返回的視圖控制器頁面,然后將導航視圖控制器推出到該頁面上。
for (UIViewController *controller in self.navigationController.viewControllers) {
BOOL isKindOfClass = [controller isKindOfClass:[FisrtViewController class]];
if (isKindOfClass) {
[self.navigationController popToViewController:controller animated:YES];
}
}
3.5 返回到根視圖控制器
導航視圖控制器中的所有子視圖控制器,都將全部出棧,從而跳轉到根視圖控制器。
- (void)popToRootViewController {
[self.navigationController popToRootViewControllerAnimated:YES];
}
4. 設置導航欄標題、字體和顏色
self.navigationItem.title = @"首頁";
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f],NSForegroundColorAttributeName:ThemeColor}];
5. 導航欄相關屬性設置
- (void)setNavigationItemAttributes {
// 設置當前視圖的導航欄標題
self.navigationItem.title = @"首頁";
self.navigationController.navigationBar.hidden = NO;
// 設置頂部導航區的提示文字,prompt 屬性表示在導航欄按鈕上方顯示的說明文字
// self.navigationItem.prompt = @"Loading";
// 設置導航欄背景是否透明
self.navigationController.navigationBar.translucent = NO;
// 設置導航欄系統樣式
// The navigation bar style that specifies its appearance.
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
// 設置導航按鈕文本顏色,默認藍色
// !!!: 此屬性設置的是全局導航欄里面的 item 項的顏色
// self.navigationController.navigationBar.tintColor = [UIColor greenColor];
}
6. 全局設置導航欄屬性
// 設置導航欄上的 item 的顏色
[[UINavigationBar appearance] setTintColor:ThemeColor];
// 設置導航欄的背景色
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
在 UINavigationBar
中,與導航欄顏色設置相關的兩個屬性:
/**
tintColor 屬性作用于 navigation items 和 bar button items
@說明:
1. tintColor 屬性的行為在 iOS 7.0 中發生了變化。它不會再影響導航欄的背景色。
2. tintColor 屬性的行為及其描述被添加到了 UIView 中。
3. 想要設置導航欄的背景顏色,請使用 barTintColor 屬性。
*/
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
/**
barTintColor 屬性作用于 navigation bar background
注:除非將半透明屬性(translucent)設置為 NO,否則默認情況下此顏色為半透明。
*/
@property(nullable, nonatomic,strong) UIColor *barTintColor API_AVAILABLE(ios(7.0)) UI_APPEARANCE_SELECTOR; // default is nil
7. 刪除導航欄底部線條
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
刪除導航欄底部線條,還有一個替代方法位于 Chameleon 框架中,該方法會把所有頁面的底部線條刪除:
self.navigationController.hidesNavigationBarHairline = YES;
8. 為導航欄右上角添加一個按鈕
導航欄 UINavigationBar
上的按鈕是 UIBarButtonItem
的實例。
#pragma mark - Lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// 添加導航欄右側按鈕
[self addNavigationRightBarbutton];
}
#pragma mark - Private
- (void)addNavigationRightBarbutton {
// 設置當前視圖右上角的導航欄按鈕標題,以及按鈕點擊事件
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"按鈕"
style:UIBarButtonItemStylePlain
target:self
action:@selector(rightBarButtonItemDidClicked:)];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}
#pragma mark - IBActions
// 導航欄按鈕點擊事件方法
- (void)rightBarButtonItemDidClicked:(id)sender {
// ...
}
9. 自定義導航按鈕:左側按鈕、右側按鈕、中間標題
#pragma mark - Private
- (void)customizeNavigationBar {
// --------------------------------------------
// 實例化一個工具條按鈕對象,它將作為我們新的導航按鈕
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(leftButtonDidClicked:)];
// 將導航欄左側按鈕,設置為新的工具條按鈕對象
self.navigationItem.leftBarButtonItem = leftButton;
// --------------------------------------------
// 同樣為導航欄右側的導航按鈕,設置新的樣式
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
target:self
action:@selector(rightButtonDidClicked:)];
self.navigationItem.rightBarButtonItem = rightButton;
// --------------------------------------------
// 創建一個視圖對象,它將作為我們導航欄的標題區
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
[titleView setBackgroundColor:[UIColor brownColor]];
// 新建一個標簽對象,它將顯示標題區的標題文字
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
label.text = @"我是自定義標題";
[titleView addSubview:label];
// 將視圖對象設置為導航欄的標題區
self.navigationItem.titleView = titleView;
}
#pragma mark - IBActions
- (void)leftButtonDidClicked:(id)sender {
NSLog(@"Left Bar Button Did Clicked!");
}
- (void)rightButtonDidClicked:(id)sender {
NSLog(@"Right Bar Button Did Clicked!");
}
10. 調整左上角返回按鈕的邊框距離
/// 直接設置
@property(nullable, nonatomic, strong) UIBarButtonItem *leftBarButtonItem;
大部分情況下,我們需要指定左邊返回按鈕距離左邊框的距離,可以如下設定:
// 【方法一】把系統返回按鈕替換成了 UIButton
UIButton *backBt = [UIButton buttonWithType:UIButtonTypeSystem];
backBt.frame = CGRectMake(0, 0, 20, 20);
[backBt setBackgroundImage:[UIImage imageNamed:@"back"]
forState:UIControlStateNormal];
[backBt addTarget:self
action:@selector(backToRootViewController)
forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBt];
// 【方法二】在系統的返回按鈕左側加了一個帶寬度的 Item
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"back"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(backToRootViewController)];
UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
// 設置邊框距離,可以根據需要調節
fixedItem.width = -16;
self.navigationItem.leftBarButtonItems = @[fixedItem, leftItem];
11. 隱藏/去掉導航欄返回按鈕文字,只顯示一個左箭頭
// 方法一:全局設置
// 隱藏返回按鈕文字,將返回按鈕的標題垂直方向向上偏移 60 pt
// 設置/獲取標題欄豎直位置偏移,UIBarMetricsDefault(豎屏)
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
// 隱藏返回按鈕文字,將返回按鈕的標題水平方向向左偏移 100 pt
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-100, 0) forBarMetrics:UIBarMetricsDefault];
// 方法二:
// 注意此法需要在前一界面內設置,而且不是全局的,但是下一個界面標題會居中
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@""
style:UIBarButtonItemStylePlain
target:self
action:nil];
- 用方法一隱藏返回按鈕的文字以后,當上一個視圖控制器的標題很長,會導致頂層視圖控制器標題不居中顯示的問題,修復的方法如下(建議做成
UIViewController
范疇(category)類):
// 如果有上個界面,將上個界面的 title 置為空,還是繞到方法二來了
- (void)resetBackButtonItem {
NSArray *viewControllerArray = [self.navigationController viewControllers];
long previousViewControllerIndex = [viewControllerArray indexOfObject:self] - 1;
UIViewController *previous;
if (previousViewControllerIndex >= 0) {
previous = [viewControllerArray objectAtIndex:previousViewControllerIndex];
previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@""
style:UIBarButtonItemStylePlain
target:self
action:nil];
}
}
- 自定義導航欄返回按鈕,將其設置為一個
UIButton
對象,然后為按鈕設置背景圖片。
// 自定義導航欄返回按鈕
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = 0;
UIButton *button = [[UIButton alloc] init];
// 圖片尺寸 22*22
[button setImage:[UIImage imageNamed:@"navigation_back_normal"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"navigation_back_hl"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(0, 0, 33, 33);
if (@available(ios 11.0,*)) {
button.contentEdgeInsets = UIEdgeInsetsMake(0, -15,0, 0);
button.imageEdgeInsets = UIEdgeInsetsMake(0, -10,0, 0);
}
[button addTarget:self
action:@selector(backButtonTapClick)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
viewController.navigationItem.leftBarButtonItems = @[backButton];
12. 把返回按鈕的文字替換為自定義文字
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"
style:UIBarButtonItemStylePlain
target:self
action:nil];
self.navigationItem.backBarButtonItem = leftItem;
以上代碼可以嵌入自定義的 UINavigationController
基類中,即:
// --------- HQLBaseNavigationViewController.h ---------
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface HQLBaseNavigationViewController : UINavigationController
@end
NS_ASSUME_NONNULL_END
// --------- HQLBaseNavigationViewController.m ---------
#import "HQLBaseNavigationViewController.h"
@interface HQLBaseNavigationViewController ()
@end
@implementation HQLBaseNavigationViewController
// 執行此方法時,統一設置下一個視圖控制器的返回按鈕
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 第一個 controller 左 button 不確定, 其他 controller 左 button 為特定樣式
if (self.viewControllers.count > 0) {
// 自定義導航欄返回按鈕文字,統一設置為“返回”,默認是上一個視圖控制器的標題
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:nil];
viewController.navigationItem.backBarButtonItem = backBarButtonItem;
// 推入下一個視圖控制器時,隱藏 TabBar 標簽欄
viewController.hidesBottomBarWhenPushed = YES;
} else {
viewController.hidesBottomBarWhenPushed = NO;
}
[super pushViewController:viewController animated:animated];
}
@end
13. 在導航欄上添加多個按鈕
// 設置導航欄返回按鈕
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@"返回"
style:UIBarButtonItemStylePlain
target:self
action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
// 設置導航欄其他按鈕
UIBarButtonItem *closeBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"關閉" style:UIBarButtonItemStylePlain target:self action:@selector(backToRootViewController)];
self.navigationItem.leftBarButtonItem = closeBarButtonItem;
// 設置左側自定義按鈕是否與返回按鈕共同存在
self.navigationItem.leftItemsSupplementBackButton = YES;
14. UINavigationControllerDelegate
// 一般用于傳遞參數,或者做一些其它處理
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
15. 歡迎頁面時隱藏狀態欄
在項目的 Info.plist
文件中添加 Status bar is initially hidden
字段并設置為 YES
,可以隱藏 App 在 LunchScreen(歡迎界面)時的狀態欄:
<key>Status bar is initially hidden<key>
<value>YES<value>
16. 修改系統狀態欄樣式
系統狀態欄樣式 UIStatusBarStyle
是一個枚舉類型:
typedef NS_ENUM(NSInteger, UIStatusBarStyle) {
// 默認樣式,自動為系統狀態欄設置白色或者黑色字體
UIStatusBarStyleDefault = 0,
// 白色狀態欄文本,適用于暗色背景
UIStatusBarStyleLightContent API_AVAILABLE(ios(7.0)) = 1,
// 黑色狀態欄文本,適用于亮色背景
UIStatusBarStyleDarkContent API_AVAILABLE(ios(13.0)) = 3,
// 以下兩個枚舉類型在 iOS 7.0 之后已失效,可以不用管
UIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1,
UIStatusBarStyleBlackOpaque NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,
} API_UNAVAILABLE(tvos);
1. 全局狀態欄樣式設置:
在 AppDelegate 文件中 添加如下設置:
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
2. preferredStatusBarStyle
方法
為當前視圖控制器添加 preferredStatusBarStyle
方法,并返回所需要的狀態欄枚舉類型:
// 設置當前視圖控制器系統狀態欄樣式
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
需要注意的是:
如果該視圖控制器沒有被
UINavigationController
所擁有,那么你可以直接在這個方法中設置當前視圖控制器的系統狀態欄樣式。如果該視圖控制器是導航視圖控制器的
viewControllers
之一,則此設置無效!
UINavigationController
不會將preferredStatusBarStyle
方法調用傳遞給它的子視圖,而是由它自己管理狀態,而且它也應該那樣做。因為UINavigationController
包含了它自己的狀態欄;因此,即使被
UINavigationController
所管理的視圖控制器實現了preferredStatusBarStyle
方法,也不會調用。
解決方法,自定義一個 UINavigationController
的子類對象,在這個子類中重寫 preferredStatusBarStyle
方法,讓其返回視圖控制器中的狀態欄設置。這樣在 UIViewController
中添加的 preferredStatusBarStyle
方法即可奏效,如下:
@implementation MyNavigationController
- (UIStatusBarStyle)preferredStatusBarStyle {
UIViewController *topViewController = self.topViewController;
return [topViewController preferredStatusBarStyle];
}
@end
3. 設置導航視圖控制器的 barStyle
屬性
// UIBarStyleBlack 為黑色導航欄,此時系統狀態欄字體為白色!
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
// 默認樣式,狀態欄字體為黑色
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
示例
在項目的 Targets — General — Deployment Info — Status Bar Style 全局狀態欄樣式設置為 Default:
在項目的 Info.plist
文件中添加如下字段,將 View controller-based status bar appearance
字段的值設置為 YES
:
<key>View controller-based status bar appearance<key>
<value>YES<value>
在指定視圖控制器頁面設置系統狀態欄樣式:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 進入當前頁面時,設置指定的狀態欄樣式
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// 退出當前頁面時,恢復原設置
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}
17. 在導航欄添加搜索框
方式一:添加 UISearchBar
// 「搜索」按鈕
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 32)];
containerView.backgroundColor = [UIColor clearColor];
containerView.layer.cornerRadius = 16;
containerView.layer.masksToBounds = YES;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:containerView.bounds];
// 設置搜索框中光標的顏色
searchBar.tintColor = [UIColor lightGrayColor];
// 搜索框背景色
searchBar.backgroundColor = [UIColor whiteColor];
searchBar.placeholder = @"搜索";
searchBar.delegate = self;
[containerView addSubview:searchBar];
containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = containerView;
// 適配 iOS 11,通過添加高度約束 44 來固定 iOS 11 中 UISearchBar 的高度
if (@available(iOS 11.0, *)) {
[searchBar.heightAnchor constraintEqualToConstant:44].active = YES;
}
方式二:添加自定義的 UIButton
// 自定義搜索按鈕
UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
searchButton.frame = CGRectMake(0, 0, 190, 32);
searchButton.layer.cornerRadius = 16;
searchButton.layer.masksToBounds = YES;
searchButton.backgroundColor = [UIColor whiteColor];
// 標題
[searchButton setTitle:@"搜索" forState:UIControlStateNormal];
searchButton.titleLabel.font = [UIFont systemFontOfSize:15];
[searchButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
// ?? 圖片,18*18
[searchButton setImage:[UIImage imageNamed:@"nav_sousuo"] forState:UIControlStateNormal];
searchButton.adjustsImageWhenHighlighted = NO;
// 設置圖片、標題左對齊
searchButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// 圖片向右移動 10pt
searchButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10.0f, 0, 0);
// 標題向右移動 20pt
searchButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15.0f, 0, 0);
[searchButton addTarget:self action:@selector(navigationSearchButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = searchButton;