// LZNavigationController.h
#import <UIKit/UIKit.h>
@interface LZNavigationController : UINavigationController
@end
// LZNavigationController.m
#import "LZNavigationController.h"
@interface LZNavigationController () <UIGestureRecognizerDelegate>
@end
@implementation LZNavigationController
+ (void)initialize
{
// 1.拿到導(dǎo)航條
UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
// 2.目的把導(dǎo)航條背景圖片
[bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
NSMutableDictionary * attr = [NSMutableDictionary dictionary];
attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
// 3.設(shè)置字體大小
[bar setTitleTextAttributes:attr];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Bug:假死狀態(tài):程序一直在跑,但是界面死了
// 在根控制器下,滑動返回, 不應(yīng)該在跟控制器的view上滑動返回
/*
// 把代理給賦空值,那么滑動手勢就不會失效了耐齐,但是會有一個問題,當(dāng)你在根控制器還側(cè)滑的時候,
// 這個時候這個手勢應(yīng)該是不應(yīng)該起作用的谤祖,誰告訴這個手勢不應(yīng)該起作用,代理,延柠,,可是代理已經(jīng)
// 死了,就沒有人讓這個手勢不起作用,所以就會造成假死狀態(tài)
self.interactivePopGestureRecognizer.delegate = nil;
*/
self.interactivePopGestureRecognizer.delegate = self;
}
#pragma mark - UIGestureRecognizerDelegate方法
// 是否觸發(fā)手勢
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// 根控制器下不要觸發(fā)手勢,讓手勢不起作用
return self.childViewControllers.count > 1 ;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// 滑動返回功能為什么失效:用滑動手勢做弃舒,驗證滑動手勢是否還在(驗證之后還在)
// 代理可以控制手勢是否有效,驗證:代理做了一些事情,導(dǎo)致滑動手勢失效
/**
* self.interactivePopGestureRecognizer =
<UIScreenEdgePanGestureRecognizer: 0x7fa2d3c5c820; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7fa2d3ea5c60>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7fa2d3c5c270>)>>
self.interactivePopGestureRecognizer.delegate =
<_UINavigationInteractiveTransition: 0x7fa7c372a850> 滑動手勢代理
*/
if (self.childViewControllers.count != 0) { // 非根控制器
viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(back) image:@"navigationButtonReturn" highImage:@"navigationButtonReturnClick" color:[UIColor blackColor] highColor:[UIColor redColor] title:@"返回"];
self.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:YES];
}
- (void)back
{
[self popViewControllerAnimated:YES];
}
@end
- 主要步驟:
- 遵守協(xié)議<UIGestureRecognizerDelegate>
- 給控制器設(shè)置代理:self.interactivePopGestureRecognizer.delegate = self;
- 實現(xiàn)代理方法
#pragma mark - UIGestureRecognizerDelegate方法
// 是否觸發(fā)手勢
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// 根控制器下不要觸發(fā)手勢,讓手勢不起作用
return self.childViewControllers.count > 1 ;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者