前言
前段时间有个小伙伴想统一设置app导航栏返回按钮的文字,但是用的方法是
[UINavigationBar appearance].backItem.title = @"返回";
来问为什么没起作用,其实是概念的不清晰,想当然以为backItem是返回按钮。故从蛮久以前的笔记里整理了这篇文章( [UINavigationBar appearance].backItem其实是nil )
基础概念
UINavigationBar管理栈上所有控制器的navigationItem(UINavigationItem),保存在items属性中,
topItem是栈顶的item,即对当前页面起作用item(当前控制器中获取self.navigationItem)
例:从A控制器push到了B控制器
B.navigationItem == B.navigationController.navigationBar.topItem
backItem是前一个控制器的navigationItem
即:A.navigationItem == B.navigationController.navigationBar.backItem
UINavigationItem继承自NSObject,相当于一个模型,
封装了当前导航bar上需要显示的所有信息。
(包含title,titleView,backBarButtonItem(UIBarButtonItem),leftBarButtonItem(UIBarButtonItem),rightBarButtonItem(UIBarButtonItem)等属性)
UIBarButtonItem是放在bar上的特殊的button,能处理点击事件
UINavigationItem对象可设置多个UIBarButtonItem对象,
例控制器中设置 :
self.navigationItem.leftBarButtonItem;
self.navigationItem.rightBarButtonItem;
当前控制器的navigationItem的backBarButtonItem决定下一个控制器的返回按钮。
即设置A.navigationItem.backBarButtonItem,实际上设置的是在B控制器中显示默认的返回按钮
设置导航栏返回按钮文字:
导航栏左侧按钮规则:
例:从A控制器 push到B控制器
- 如果B控制器有自定义的leftBarButtonItem,则显示这个leftBarButtonItem;
- 如果B控制器没有,看A控制器backBarButtonItem属性是否有自定义项,有则显示;
- 12都没有,则默认显示一个返回按钮,文字是A控制器的标题。
方法一:
在B控制器中设置leftBarButtonItem。
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
self.navigationItem.leftBarButtonItem = leftItem;
注:如果给控制器添加了leftBarButtonItem,系统侧滑返回手势会失效
方法二:
在A控制器中设置backBarButtonItem,实际上设置的就是B控制器上默认显示的返回按钮
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
**注:self.navigationItem.backBarButtonItem.title = @"返回”; 直接这样设置无效,
title需要item设置到bar之前修改,而backBarButtonItem是系统内置的,所以直接改无效,而且如果没有手动设置backBarButtonItem,获取到的backBarButtonItem也是nil。
方法三:利用runtime一次设置全部控制器
1、建一个UIViewController分类
2、在分类的+load方法里用method swizzling来hook控制器的viewDidLoad方法
3、同方法二统一设置
文字设置为空的做法也可以设置为创建title是空字符串的UIBarButtonItem
网上也有做法是设置文字偏移到屏幕外:(据说会有闪屏的可能?)
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -100)
forBarMetrics:UIBarMetricsDefault];
统一设置导航栏返回按钮图片:
UIImage *buttonBackImg = [UIImage imageNamed:@"ic_navbar_back-black_normal”];
// 可以设置图片不要渲染
// buttonBackImg = [buttonBackImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[[UINavigationBar appearance] setBackIndicatorImage:buttonBackImg];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:buttonBackImg];
处理设置了leftBarButtonItem后侧滑返回失效/控制器中scrollview与侧滑共存问题:
核心就是自定义一个导航控制器,并作为手势对象的代理和导航控制器的代理,同时处理手势冲突问题
#import “NavViewController.h"
@interface NavViewController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>
@end
@implementation NavViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
__weak __typeof(self) weakSelf = self;
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
// UIScreenEdgePanGestureRecognizer
self.interactivePopGestureRecognizer.delegate = weakSelf;
self.delegate = weakSelf;
}
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = NO;
[super pushViewController:viewController animated:animated];
}
#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animate
{
// Enable the gesture again once the new controller is shown
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = YES;
}
#pragma mark UINavigationControllerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([self.childViewControllers count] == 1) {
return NO;
}
return YES;
}
// ViewController接受多个手势,再处理手势冲突问题。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
// 处理手指在滑动的时候,被pop的 ViewController 中的 UIScrollView 会跟着一起滚动问题
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}
@end
导航栏自定义
// 底部阴影
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
// 背景图片
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
//bar背景颜色
[[UINavigationBar appearance] setBarTintColor:[UIColor clearColor]];
// 文字颜色
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// 标题位置偏移
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:10 forBarMetrics:UIBarMetricsDefault];
// 标题字体
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 0);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:16], NSFontAttributeName,
shadow,NSShadowAttributeName, nil]];
// 返回图标
[UINavigationBar appearance].backIndicatorImage =
[UINavigationBar appearance].backIndicatorTransitionMaskImage =
[[UIImage imageNamed:@"ic_navbar_back-black_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];