iOSNavigationBarHidden
iOS导航栏隐藏/显示,手势返回动画流畅
###解决方案
step1:通过运行时添加属性shouldNavigationBarHidden
,标识控制器是否需要隐藏导航栏
step2:自定义导航栏,对比shouldNavigationBarHidden
与导航栏navigationBarHidden
,不一致时隐藏导航栏
###使用方法:
在ViewController的viewDidLoad
中设置
self.shouldNavigationBarHidden
即可。
注意:push
前设置view.backgroundColor
等view
相关内容会导致viewDidLoad
提前的顺序问题
###代码
@interface UIViewController (NavigationBarHidden)
/// 是否隐藏导航栏。默认NO。
@property (nonatomic , assign) BOOL shouldNavigationBarHidden;
@end
@interface HideNavigationController : UINavigationController<UINavigationControllerDelegate>
@end
#import "UIViewController+NavigationBarHidden.h"
#import <objc/runtime.h>
@implementation UIViewController (NavigationBarHidden)
- (BOOL)shouldNavigationBarHidden{
return [objc_getAssociatedObject(self, @selector(shouldNavigationBarHidden)) boolValue];
}
- (void)setShouldNavigationBarHidden:(BOOL)shouldNavigationBarHidden{
objc_setAssociatedObject(self, @selector(shouldNavigationBarHidden), @(shouldNavigationBarHidden), OBJC_ASSOCIATION_ASSIGN);
}
@end
@implementation HideNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.viewControllers.count > 0) {
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (viewController.shouldNavigationBarHidden != self.navigationBarHidden) {
[self setNavigationBarHidden:viewController.shouldNavigationBarHidden animated:animated];
}
}
@end
对于隐藏了导航栏的控制器,手势返回也消失了。可自定义手势,在边缘开始滑动时调用interactivePopGestureRecognizer
方法。可参考 iOS导航栏隐藏透明处理 && 侧滑返回手势