// // MyUINavigationController.m // jiaPeiC // // Created by Echopentakill on 16/12/13. // Copyright © 2016年 JCZ. All rights reserved. // #import "MyUINavigationController.h" @interface MyUINavigationController () @end @implementation MyUINavigationController // 第一次使用这个类调用一次 + (void)initialize { // 2.设置UINavigationBar的主题 [self rq_setupNavigationBarTheme]; // 3.设置UIBarButtonItem的主题 [self rq_setupBarButtonItemTheme]; } - (void)viewDidLoad { [super viewDidLoad]; __weak MyUINavigationController *weakSelf = self; if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.interactivePopGestureRecognizer.delegate = weakSelf; self.delegate = weakSelf; } } /** * 设置UINavigationBarTheme的主题 */ + (void)rq_setupNavigationBarTheme { UINavigationBar *appearance = [UINavigationBar appearance]; /// 设置背景 //!!!: 必须设置为透明 不然布局有问题 ios8以下 会崩溃/ 如果iOS8以下 请再_setup里面 设置透明 self.navigationBar.translucent = YES; [appearance setTranslucent:YES]; /// 必须设置YES // 设置导航栏的样式 [appearance setBarStyle:UIBarStyleDefault]; //设置导航栏文字按钮的渲染色 [appearance setTintColor:RQColorFromHexString(@"#333333")]; // 设置导航栏的背景渲染色 [appearance setBarTintColor:RQMianColor]; // 设置文字属性 NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary]; textAttrs[NSFontAttributeName] = [UIFont fontWithName:@"Helvetica-Bold" size:17.f]; textAttrs[NSForegroundColorAttributeName] = RQColorFromHexString(@"#ffffff"); // UIOffsetZero是结构体, 只要包装成NSValue对象, 才能放进字典\数组中 NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowOffset = CGSizeZero; textAttrs[NSShadowAttributeName] = shadow; [appearance setTitleTextAttributes:textAttrs]; /// 去掉导航栏的阴影图片 [appearance setShadowImage:[UIImage new]]; [appearance setBackgroundImage:[UIImage yy_imageWithColor:RQMianColor] forBarMetrics:UIBarMetricsDefault]; } /** * 设置UIBarButtonItem的主题 */ + (void)rq_setupBarButtonItemTheme { // 通过appearance对象能修改整个项目中所有UIBarButtonItem的样式 UIBarButtonItem *appearance = [UIBarButtonItem appearance]; CGFloat fontSize = 16.0f; /**设置文字属性**/ // 设置普通状态的文字属性 NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary]; textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor]; textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:fontSize]; NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowOffset = CGSizeZero; textAttrs[NSShadowAttributeName] = shadow; [appearance setTitleTextAttributes:textAttrs forState:UIControlStateNormal]; // 设置高亮状态的文字属性 NSMutableDictionary *highTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs]; highTextAttrs[NSForegroundColorAttributeName] = [[UIColor whiteColor] colorWithAlphaComponent:.5f]; [appearance setTitleTextAttributes:highTextAttrs forState:UIControlStateHighlighted]; // 设置不可用状态(disable)的文字属性 NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs]; disableTextAttrs[NSForegroundColorAttributeName] = [[UIColor whiteColor] colorWithAlphaComponent:.5f]; [appearance setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled]; } //在转场/过渡的时候禁用 interactivePopGestureRecognizer当用户在转场的时候触发一个后退手势,则各种事件又凑一块了.导航栈内又成了混乱的.我的解决办法是,转场效果的过程中禁用手势识别,当新的视图控制器加载完成后再启用.再次建议使用UINavigationController的子类操作 -(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) self.interactivePopGestureRecognizer.enabled = NO; [super pushViewController:viewController animated:animated]; } #pragma mark UINavigationControllerDelegate - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { // Enable the gesture again once the new controller is shown if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) self.interactivePopGestureRecognizer.enabled = YES; } // //解决左滑手势冲突和不灵敏的问题 -(UIViewController *)popViewControllerAnimated:(BOOL)animated { return [super popViewControllerAnimated:YES]; } #pragma mark UIGestureRecognizerDelegate - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ if ([self.childViewControllers count] == 1) { return NO; } return YES; } // 我们差不多能猜到是因为手势冲突导致的,那我们就先让 ViewController 同时接受多个手势吧 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } //运行试一试,两个问题同时解决,不过又发现了新问题,手指在滑动的时候,被 pop 的 ViewController 中的 UIScrollView 会跟着一起滚动,这个效果看起来就很怪(知乎日报现在就是这样的效果),而且也不是原始的滑动返回应有的效果,那么就让我们继续用代码来解决吧 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end