效果圖 Gif
1.自定義leftBarButtonItem , 自定義 RigthBarButtonItem , 自定義中間的 titleView ;
2.修改返回按鈕到左邊的寬度太大的問(wèn)題:(UIBarButtonSystemItemFixedSpace)
我單獨(dú)設(shè)置出一個(gè)空 button 來(lái)占位,并且寬度設(shè)置為-20 , 這樣數(shù)組中第二個(gè)返回按鈕的位置就會(huì)相應(yīng)的往左側(cè)移動(dòng)了!
UIBarButtonItem *negSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
3.leftBackItem 與 closeItem 的寬度是改不了的~因?yàn)樗麄兊膶挾仁歉鶕?jù) image 自適應(yīng)的 , 所以修改他們之間的距離不應(yīng)該從 width 屬性來(lái)修改 , 應(yīng)該在 UI 切圖的時(shí)候去審慎定奪;
效果圖 Gif.gif
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//全局統(tǒng)一設(shè)置:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBack"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
UIImage *backImage = [[UIImage imageNamed:@"imgBack"] imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, -3, 0)];
//返回箭頭王上挪動(dòng)3;
[[UINavigationBar appearance] setBackIndicatorImage:backImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backImage];
//或者:讓箭頭右側(cè)的文字網(wǎng)上走一點(diǎn)兒:負(fù)數(shù)向上移動(dòng);
// [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -1.5f) forBarMetrics:UIBarMetricsDefault];
return YES;
}
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"第一頁(yè)";
// [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;
//替換分割線的時(shí)候前提是背景圖也要被替換才行:(setBackgroundImage: forBarMetrics:)這個(gè)方法:
// [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];
// //富文本定義標(biāo)題:
// [self.navigationController.navigationBar setTitleTextAttributes:navBarTitleTextAttributes];
//
//
// //標(biāo)題垂直偏移:
// [self.navigationController.navigationBar setTitleVerticalPositionAdjustment:10.0f forBarMetrics:UIBarMetricsDefault];
//
// //修改返回按鈕的顏色:
// self.navigationController.navigationBar.tintColor = [UIColor blackColor];
// //修改返回按鈕圖片:(這兩個(gè)要同時(shí)設(shè)置);
// [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 設(shè)置為 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 = @"第二頁(yè)";
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)];
//修改返回按鈕到左邊的寬度太大的問(wèn)題:(UIBarButtonSystemItemFixedSpace)
//我單獨(dú)設(shè)置出一個(gè)空 button 來(lái)占位,并且寬度設(shè)置為-20 , 這樣數(shù)組中第二個(gè)返回按鈕的位置就會(huì)相應(yīng)的往左側(cè)移動(dòng)了!
// UIBarButtonItem *negSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
// negSpaceItem.width = -10;
//leftBackItem 與 closeItem 的寬度是改不了的~因?yàn)樗麄兊膶挾仁歉鶕?jù) image 自適應(yīng)的 , 所以修改他們之間的距離不應(yīng)該從 width 屬性來(lái)修改 , 應(yīng)該在 UI 切圖的時(shí)候去審慎定奪;
//右側(cè)分享按鈕:
// 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(@"點(diǎn)擊關(guān)閉按鈕");
}
- (void)shareAction {
[self.navigationController popViewControllerAnimated:YES];
NSLog(@"點(diǎn)擊分享按鈕");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end