单页面隐藏 Navigation Bar,同时保持右滑手势退出页面的终极方案

1. 隐藏 navigation bar

在需要隐藏 navigation bar 的 view controller 中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self hideNavBar];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self showNavBar];
}

- (void)hideNavBar
{
    if (self.navigationController.viewControllers.lastObject == self) //避免推出下一个隐藏bar bar的vc过场动画展示了nav bar
    {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
}

- (void)showNavBar
{
    if (self.navigationController.viewControllers.lastObject == self) //避免推出下一个隐藏bar bar的vc过场动画展示了nav bar
    {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

2. 把手势搞出来

新增两个类:

1) OTInteractivePopRecognizerDelegate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@interface OTInteractivePopRecognizerDelegate : NSObject <UIGestureRecognizerDelegate>
@property (nonatomic, weak) UINavigationController *navigationController;
@end

@implementation OTInteractivePopRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return self.navigationController.viewControllers.count > 1;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

@end

2) OTNavigationPopGestureUtil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#import <objc/runtime.h>

@interface OTNavigationPopGestureUtil : NSObject

+ (void)protectNavigationController:(UINavigationController *)navigationController;

@end

@implementation OTNavigationPopGestureUtil

+ (void)protectNavigationController:(UINavigationController *)navigationController
{
    OTInteractivePopRecognizerDelegate *popRecognizer = objc_getAssociatedObject(navigationController, _cmd);
    if (!popRecognizer)
    {
        popRecognizer = [[OTInteractivePopRecognizerDelegate alloc] init];
        popRecognizer.navigationController = navigationController;
        navigationController.interactivePopGestureRecognizer.delegate = popRecognizer;
        objc_setAssociatedObject(navigationController, _cmd, popRecognizer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}

@end

在需要隐藏 navigation bar 的 view controller 中:

1
2
3
4
5
- (void)viewDidLoad
{
    [super viewDidLoad];
    [OTNavigationPopGestureUtil protectNavigationController:self.navigationController];
}

Over

Comments