RQBaseNavigationController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // RQBaseNavigationController.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/13.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "RQBaseNavigationController.h"
  9. //#import "RQBaseViewController.h"
  10. @interface RQBaseNavigationController ()
  11. @property (nonatomic , weak , readwrite) UIImageView * navigationBottomLine;// 导航栏分隔线
  12. @end
  13. @implementation RQBaseNavigationController
  14. // 第一次使用这个类调用一次
  15. + (void)initialize {
  16. // 2.设置UINavigationBar的主题
  17. // [self _setupNavigationBarTheme];
  18. // 3.设置UIBarButtonItem的主题
  19. // [self _setupBarButtonItemTheme];
  20. }
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. // 初始化
  24. // [self _setup];
  25. }
  26. #pragma mark - 初始化
  27. - (void) _setup {
  28. [self _setupNavigationBarBottomLine];
  29. if (@available(iOS 15.0, *)) {
  30. UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
  31. [appearance configureWithOpaqueBackground];
  32. /// 设置导航栏背景色
  33. appearance.backgroundColor = RQ_MAIN_BACKGROUNDCOLOR;
  34. /// 设置文字属性
  35. NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
  36. textAttrs[NSFontAttributeName] = RQMediumFont(15.0f);
  37. textAttrs[NSForegroundColorAttributeName] = RQ_MAIN_TEXT_COLOR_1;
  38. appearance.titleTextAttributes = textAttrs;
  39. /// 设置导航栏下边界分割线透明
  40. appearance.shadowImage = [UIImage imageWithColor:UIColor.clearColor];
  41. self.navigationBar.standardAppearance = appearance;
  42. self.navigationBar.scrollEdgeAppearance = self.navigationBar.standardAppearance;
  43. } else {
  44. // Fallback on earlier versions
  45. }
  46. }
  47. // 查询最后一条数据
  48. - (UIImageView *)_findHairlineImageViewUnder:(UIView *)view {
  49. if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
  50. return (UIImageView *)view;
  51. }
  52. for (UIView *subview in view.subviews){
  53. UIImageView *imageView = [self _findHairlineImageViewUnder:subview];
  54. if (imageView){ return imageView; }
  55. }
  56. return nil;
  57. }
  58. #pragma mark - 设置导航栏的分割线
  59. - (void)_setupNavigationBarBottomLine {
  60. //!!!:这里之前设置系统的 navigationBarBottomLine.image = xxx;无效 Why? 隐藏了系统的 自己添加了一个分割线
  61. // 隐藏系统的导航栏分割线
  62. UIImageView *navigationBarBottomLine = [self _findHairlineImageViewUnder:self.navigationBar];
  63. navigationBarBottomLine.hidden = YES;
  64. // 添加自己的分割线
  65. CGFloat navSystemLineH = .5f;
  66. UIImageView *navSystemLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.navigationBar.rq_height - navSystemLineH, RQ_SCREEN_WIDTH, navSystemLineH)];
  67. navSystemLine.backgroundColor = RQ_MAIN_LINE_COLOR_1;
  68. [self.navigationBar addSubview:navSystemLine];
  69. self.navigationBottomLine = navSystemLine;
  70. }
  71. /**
  72. * 设置UINavigationBarTheme的主题
  73. */
  74. + (void)_setupNavigationBarTheme {
  75. UINavigationBar *appearance = [UINavigationBar appearance];
  76. /// 设置背景
  77. //!!!: 必须设置为透明 不然布局有问题 ios8以下 会崩溃/ 如果iOS8以下 请再_setup里面 设置透明 self.navigationBar.translucent = YES;
  78. [appearance setTranslucent:YES]; /// 必须设置YES
  79. // 设置导航栏的样式
  80. [appearance setBarStyle:UIBarStyleDefault];
  81. //设置导航栏文字按钮的渲染色
  82. [appearance setTintColor:RQ_MAIN_TEXT_COLOR_1];
  83. // 设置导航栏的背景渲染色
  84. [appearance setBarTintColor:RQ_MAIN_BACKGROUNDCOLOR];
  85. // 设置文字属性
  86. NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
  87. textAttrs[NSFontAttributeName] = RQMediumFont(17.0f);
  88. textAttrs[NSForegroundColorAttributeName] = RQ_MAIN_TEXT_COLOR_1;
  89. // UIOffsetZero是结构体, 只要包装成NSValue对象, 才能放进字典\数组中
  90. NSShadow *shadow = [[NSShadow alloc] init];
  91. shadow.shadowOffset = CGSizeZero;
  92. textAttrs[NSShadowAttributeName] = shadow;
  93. [appearance setTitleTextAttributes:textAttrs];
  94. /// 去掉导航栏的阴影图片
  95. [appearance setShadowImage:[UIImage new]];
  96. }
  97. /**
  98. * 设置UIBarButtonItem的主题
  99. */
  100. + (void)_setupBarButtonItemTheme{
  101. // 通过appearance对象能修改整个项目中所有UIBarButtonItem的样式
  102. UIBarButtonItem *appearance = [UIBarButtonItem appearance];
  103. CGFloat fontSize = 16.0f;
  104. /**设置文字属性**/
  105. // 设置普通状态的文字属性
  106. NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
  107. textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
  108. textAttrs[NSFontAttributeName] = RQRegularFont(fontSize);
  109. NSShadow *shadow = [[NSShadow alloc] init];
  110. shadow.shadowOffset = CGSizeZero;
  111. textAttrs[NSShadowAttributeName] = shadow;
  112. [appearance setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
  113. // 设置高亮状态的文字属性
  114. NSMutableDictionary *highTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
  115. highTextAttrs[NSForegroundColorAttributeName] = [[UIColor whiteColor] colorWithAlphaComponent:.5f];
  116. [appearance setTitleTextAttributes:highTextAttrs forState:UIControlStateHighlighted];
  117. // 设置不可用状态(disable)的文字属性
  118. NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
  119. disableTextAttrs[NSForegroundColorAttributeName] = [[UIColor whiteColor] colorWithAlphaComponent:.5f];
  120. [appearance setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
  121. }
  122. #pragma mark - Publi Method
  123. /// 显示导航栏的细线
  124. - (void)rq_showNavigationBottomLine {
  125. self.navigationBottomLine.hidden = NO;
  126. }
  127. /// 隐藏导航栏的细线
  128. - (void)rq_hideNavigationBottomLine {
  129. self.navigationBottomLine.hidden = YES;
  130. }
  131. /// 能拦截所有push进来的子控制器
  132. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
  133. // 如果现在push的不是栈底控制器(最先push进来的那个控制器)
  134. if (self.viewControllers.count > 0){
  135. /// 隐藏底部tabbar
  136. viewController.hidesBottomBarWhenPushed = YES;
  137. NSString *title = @"";
  138. /// eg: [A push B]
  139. /// 1.取出当前的控制器的title , 也就是取出 A.title
  140. /// RQ Fixed: [[self topViewController] navigationItem].title 这样来获取title 而不是[[self topViewController] title]
  141. title = [[self topViewController] navigationItem].title? : @"";
  142. /// 2.判断要被Push的控制器(B)是否是 RQViewController ,
  143. if ([viewController isKindOfClass:[RQBaseViewController class]]) {
  144. RQBaseViewModel *viewModel = [(RQBaseViewController *)viewController viewModel];
  145. /// 3. 查看backTitle 是否有值
  146. title = viewModel.backTitle?:title;
  147. }
  148. // 4.这里可以设置导航栏的左右按钮 统一管理方法
  149. // viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem rq_backItemWithTitle:@"" imageName:@"backIcon" target:self action:@selector(rq_back)];
  150. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backIcon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rq_back)];
  151. // [backItem setTintColor:RQ_MAIN_COLOR];
  152. viewController.navigationItem.leftBarButtonItem = backItem;
  153. }
  154. if ([self.viewControllers.lastObject isKindOfClass:[viewController class]]) {
  155. return;
  156. }
  157. // push
  158. [super pushViewController:viewController animated:animated];
  159. }
  160. /// 事件处理
  161. - (void)rq_back {
  162. [self popViewControllerAnimated:YES];
  163. }
  164. #pragma mark - Override
  165. //- (BOOL)shouldAutorotate {
  166. // return self.topViewController.shouldAutorotate;
  167. //}
  168. //
  169. //- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
  170. // return self.topViewController.supportedInterfaceOrientations;
  171. //}
  172. //
  173. //- (UIStatusBarStyle)preferredStatusBarStyle{
  174. // return self.topViewController.preferredStatusBarStyle;
  175. //}
  176. //
  177. //- (BOOL)prefersStatusBarHidden{
  178. // return self.topViewController.prefersStatusBarHidden;
  179. //}
  180. @end