效果图 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 切图的时候去审慎定夺;
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