1.自定義返回按鈕的同時(shí), 保持iOS原生滑動(dòng)返回手勢(shì)(interactivePopGestureRecognizer)可用的方法
注意:
(1) 這樣做會(huì)導(dǎo)致導(dǎo)航欄混亂, 即一個(gè)頁(yè)面可能顯示別的頁(yè)面的導(dǎo)航欄, 甚至更嚴(yán)重的bug,效果如下:
(2) 如果只是單純重設(shè)interactivePopGestureRecognizer的代理, 在push的過(guò)程中觸發(fā)滑動(dòng)返回會(huì)崩潰(好像還有別的問(wèn)題), 所以需要在push動(dòng)畫過(guò)程中禁掉interactivePopGestureRecognizer手勢(shì)
F5CAFB1B-672C-4258-BAF4-A3D19512F80A.png
1C9AC2E2-4C9D-4845-BF11-330FE8E3AB31.png
//
// 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)后, 滑動(dòng)返回不可用, 使用這種方式處理
__weak typeof (self) weakSelf = self;
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.delegate = weakSelf;
}
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 禁用返回手勢(shì), 防止崩潰
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];
}
// 返回手勢(shì)可用
- (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
- '更換返回按鈕箭頭并去掉返回按鈕文字'的方法, 這樣不會(huì)出現(xiàn)奇怪的bug,
缺點(diǎn):
(1). 是返回按鈕樣式自由度不夠高
(2). 隱藏導(dǎo)航欄后, 滑動(dòng)返回不可用
//
// 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]];
// 設(shè)置返回按鈕箭頭
[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