效果圖 Gif
1.自定義leftBarButtonItem , 自定義 RigthBarButtonItem , 自定義中間的 titleView ;
2.修改返回按鈕到左邊的寬度太大的問題:(UIBarButtonSystemItemFixedSpace)
我單獨設置出一個空 button 來占位,并且寬度設置為-20 , 這樣數組中第二個返回按鈕的位置就會相應的往左側移動了!
UIBarButtonItem *negSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
3.leftBackItem 與 closeItem 的寬度是改不了的~因為他們的寬度是根據 image 自適應的 , 所以修改他們之間的距離不應該從 width 屬性來修改 , 應該在 UI 切圖的時候去審慎定奪;
效果圖 Gif.gif
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//全局統一設置:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBack"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
UIImage *backImage = [[UIImage imageNamed:@"imgBack"] imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, -3, 0)];
//返回箭頭王上挪動3;
[[UINavigationBar appearance] setBackIndicatorImage:backImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backImage];
//或者:讓箭頭右側的文字網上走一點兒:負數向上移動;
// [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -1.5f) forBarMetrics:UIBarMetricsDefault];
return YES;
}
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"第一頁";
// [self.navigationController.navigationBar setTranslucent:NO];
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBack"] forBarMetrics:UIBarMetricsDefault];
// self.navigationController.navigationBar.backgroundColor = [UIColor grayColor];
// [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
// self.view.backgroundColor = [UIColor blueColor];
// [self.navigationController.navigationBar setTranslucent:NO];
// self.navigationController.navigationBar.clipsToBounds = YES;
//替換分割線的時候前提是背景圖也要被替換才行:(setBackgroundImage: forBarMetrics:)這個方法:
// [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"navSep"]];
//
// //NSShadowAttributeName:
// NSShadow *shadow = [NSShadow new];
// [shadow setShadowColor:[UIColor colorWithWhite:0.0f alpha:0.75f]];
// [shadow setShadowOffset:CGSizeMake(1.0f, 1.0f)];
// [shadow setShadowBlurRadius:1.0f];
//
// NSDictionary *navBarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor yellowColor], NSForegroundColorAttributeName, shadow, NSShadowAttributeName, nil];
// //富文本定義標題:
// [self.navigationController.navigationBar setTitleTextAttributes:navBarTitleTextAttributes];
//
//
// //標題垂直偏移:
// [self.navigationController.navigationBar setTitleVerticalPositionAdjustment:10.0f forBarMetrics:UIBarMetricsDefault];
//
// //修改返回按鈕的顏色:
// self.navigationController.navigationBar.tintColor = [UIColor blackColor];
// //修改返回按鈕圖片:(這兩個要同時設置);
// [self.navigationController.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"imgBack"]];
// [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"imgBack"]];
// //隱藏:
// [self.navigationController.navigationBar setHidden:YES];
//View controller-based status bar appearance 設置為 NO:
// [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
//不在推薦使用的方式:
// [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
NextViewController.m
#import "NextViewController.h"
@interface NextViewController ()
@end
@implementation NextViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"第二頁";
self.view.backgroundColor = [UIColor whiteColor];
// UIBarButtonItem *leftBackItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"imgBack"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
// self.navigationController.navigationBar.tintColor = [UIColor blackColor];
// UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navClose"] style:UIBarButtonItemStylePlain target:self action:@selector(closeAction)];
//修改返回按鈕到左邊的寬度太大的問題:(UIBarButtonSystemItemFixedSpace)
//我單獨設置出一個空 button 來占位,并且寬度設置為-20 , 這樣數組中第二個返回按鈕的位置就會相應的往左側移動了!
// UIBarButtonItem *negSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
// negSpaceItem.width = -10;
//leftBackItem 與 closeItem 的寬度是改不了的~因為他們的寬度是根據 image 自適應的 , 所以修改他們之間的距離不應該從 width 屬性來修改 , 應該在 UI 切圖的時候去審慎定奪;
//右側分享按鈕:
// UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navShare"] style:UIBarButtonItemStylePlain target:self action:@selector(shareAction)];
// UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 44)];
// customView.backgroundColor = [UIColor yellowColor];
// UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
//
// self.navigationItem.leftBarButtonItems = @[negSpaceItem, leftBackItem, customItem,closeItem];
// self.navigationItem.rightBarButtonItems = @[rightItem];
//
// UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
// titleView.backgroundColor = [UIColor cyanColor];
// self.navigationItem.titleView = titleView;
}
- (void)goBack {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)closeAction {
[self.navigationController popViewControllerAnimated:YES];
NSLog(@"點擊關閉按鈕");
}
- (void)shareAction {
[self.navigationController popViewControllerAnimated:YES];
NSLog(@"點擊分享按鈕");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end