案例一:实现全屏滑动
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"替换系统手势";
NSArray *targetsArr = [self.navigationController.interactivePopGestureRecognizer valueForKey:@"targets"];
id target = [[targetsArr lastObject] valueForKey:@"target"];
SEL actionSEL = NSSelectorFromString(@"handleNavigationTransition:");
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:target action:actionSEL];
[self.view addGestureRecognizer:panGesture];
panGesture.delegate = self;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return self.navigationController.childViewControllers.count != 1;
}
案例二:去掉导航栏下的线
UIView * backgroundView = [self.navigationBar subviews].firstObject;
UIView * navLine = [backgroundView.subviews firstObject];
navLine.hidden = YES;
案例三:隐藏tabbar
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.viewControllers.count > 0) {
//viewController是将要被push的控制器
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
案例三:隐藏导航栏返回文字
//ios11 导致箭头下偏
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(510, 550) forBarMetrics:UIBarMetricsDefault];
//可以采用如下方法
if (@available(iOS 11 ,*)) {
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-300, 0) forBarMetrics:UIBarMetricsDefault];
}else{
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -300) forBarMetrics:UIBarMetricsDefault];
}
案例四:改变状态栏字体颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
//白色
return UIStatusBarStyleLightContent;
}
案例五:隐藏返回按钮
[self.navigationItem setHidesBackButton:YES animated:YES];
案例六:打开手势
self.navigationController.interactivePopGestureRecognizer.delegate = self;
#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
案例七:导航栏标题字体颜色及其大小
self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithHex:0x333333 alpha:1],NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
案例八:改变导航栏图标的颜色
self.navigationBar.tintColor = [UIColor colorWithHex:333333 alpha:1];
案例九:隐藏导航栏
第一种做法
-注意这里一定要用动画的方式隐藏导航栏,这样在使用滑动返回手势的时候效果最好,和上面动图一致.这样做有一个缺点就是在切换tabBar的时候有一个导航栏向上消失的动画.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
第二种做法
设置self为导航控制器的代理,实现代理方法,在将要显示控制器中设置导航栏隐藏和显示,使用这种方式不仅完美切合滑动返回手势,同时也解决了切换tabBar的时候,导航栏动态隐藏的问题.
@interface WLHomePageController () <UINavigationControllerDelegate>
@end
@implementation WLHomePageController
#pragma mark - lifeCycle
- (void)viewDidLoad {
[super viewDidLoad];
// 设置导航控制器的代理为self
self.navigationController.delegate = self;
}
#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 判断要显示的控制器是否是自己
BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
[self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
案例十:UIScrollView
手势与侧滑手势冲突
NSArray *gestureArray = self.navigationController.view.gestureRecognizers;
for (UIGestureRecognizer *gestureRecognizer in gestureArray) {
if ([gestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
[_scrollView.panGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer];
}
}
案例十一:在UINavigationController的rootViewController上滑动返回时,会出现界面卡死的情况。在导航栏的基类中作如下处理。
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
// 屏蔽调用rootViewController的滑动返回手势
if (self.viewControllers.count < 2 || self.visibleViewController == [self.viewControllers objectAtIndex:0]) {
return NO;
}
}
return YES;
}