1.自定义返回按钮的同时, 保持iOS原生滑动返回手势(interactivePopGestureRecognizer)可用的方法
注意:
(1) 这样做会导致导航栏混乱, 即一个页面可能显示别的页面的导航栏, 甚至更严重的bug,效果如下:
(2) 如果只是单纯重设interactivePopGestureRecognizer的代理, 在push的过程中触发滑动返回会崩溃(好像还有别的问题), 所以需要在push动画过程中禁掉interactivePopGestureRecognizer手势
//
// SENavigationViewController.m
// SpeakEnglish
//
// Created by Daniel on 16/4/1.
// Copyright © 2016年 Daniel. All rights reserved.
//
#import "SENavigationViewController.h"
#import "UIImage+ZY.h"
@interface SENavigationViewController ()<UIGestureRecognizerDelegate, UINavigationControllerDelegate>
@end
@implementation SENavigationViewController
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
if (self = [super initWithRootViewController:rootViewController]) {
[self setNavigationBarTheme];
self.delegate = self;
}
return self;
}
// 主题
- (void)setNavigationBarTheme {
// title属性
UINavigationBar *bar = [UINavigationBar appearance];
NSDictionary *attr = @{NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:FontTitle};
[bar setTitleTextAttributes:attr];
// 背景颜色
[bar setBackgroundImage:[UIImage createImageWithColor:HEXCOLORV(ColorMain)] forBarMetrics:UIBarMetricsDefault];
[bar setShadowImage:[UIImage createImageWithColor:HEXCOLORV(ColorMain)]];
// 按钮图片渲染
[bar setTintColor:[UIColor whiteColor]];
// 按钮属性
UIBarButtonItem *item = [UIBarButtonItem appearance];
NSDictionary *itemAttr = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]};
[item setTitleTextAttributes:itemAttr forState:UIControlStateNormal];
[item setTintColor:[UIColor whiteColor]];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 自定义返回键(leftItem)后, 滑动返回不可用, 使用这种方式处理
__weak typeof (self) weakSelf = self;
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.delegate = weakSelf;
}
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 禁用返回手势, 防止崩溃
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled = NO;
}
// 隐藏tabBar
if (self.viewControllers.count == 1) {
viewController.hidesBottomBarWhenPushed = YES;
}
// 定制返回键
if (self.viewControllers.count >= 1) {
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 20)];
[btn setImage:[UIImage templateImageWithName:@"nav_back"] forState:UIControlStateNormal];
[btn.imageView setContentMode:UIViewContentModeScaleAspectFit];
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, -50, 0, 0)];
[btn addTarget:self action:@selector(popSelector) forControlEvents:UIControlEventTouchUpInside];
btn.tintColor = [UIColor whiteColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
viewController.navigationItem.leftBarButtonItem = item;
}
[super pushViewController:viewController animated:animated];
}
// 返回手势可用
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animated {
if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
- (void)popSelector {
[self popViewControllerAnimated:YES];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return self.visibleViewController.preferredStatusBarStyle;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
- '更换返回按钮箭头并去掉返回按钮文字'的方法, 这样不会出现奇怪的bug,
缺点:
(1). 是返回按钮样式自由度不够高
(2). 隐藏导航栏后, 滑动返回不可用
//
// SENavigationViewController.m
// SpeakEnglish
//
// Created by Daniel on 16/4/1.
// Copyright © 2016年 Daniel. All rights reserved.
//
#import "SENavigationViewController.h"
#import "UIImage+ZY.h"
@interface SENavigationViewController ()<UIGestureRecognizerDelegate, UINavigationControllerDelegate>
@end
@implementation SENavigationViewController
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
if (self = [super initWithRootViewController:rootViewController]) {
[self setNavigationBarTheme];
self.delegate = self;
}
return self;
}
// 主题
- (void)setNavigationBarTheme {
// title属性
UINavigationBar *bar = [UINavigationBar appearance];
NSDictionary *attr = @{NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:FontTitle};
[bar setTitleTextAttributes:attr];
// 背景颜色
[bar setBackgroundImage:[UIImage createImageWithColor:HEXCOLORV(ColorMain)] forBarMetrics:UIBarMetricsDefault];
[bar setShadowImage:[UIImage createImageWithColor:HEXCOLORV(ColorMain)]];
// 按钮图片渲染
[bar setTintColor:[UIColor whiteColor]];
// 设置返回按钮箭头
[bar setBackIndicatorImage:[UIImage imageNamed:@"nav_back"]];
[bar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"nav_back"]];
// 按钮属性
UIBarButtonItem *item = [UIBarButtonItem appearance];
NSDictionary *itemAttr = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]};
[item setTitleTextAttributes:itemAttr forState:UIControlStateNormal];
[item setTintColor:[UIColor whiteColor]];
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 隐藏tabBar
if (self.viewControllers.count == 1) {
viewController.hidesBottomBarWhenPushed = YES;
}
// 去掉返回按钮文字
UIBarButtonItem *backitem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
viewController.navigationItem.backBarButtonItem = backitem;
[super pushViewController:viewController animated:animated];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return self.visibleViewController.preferredStatusBarStyle;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end