RDSNavController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // RDSNavController.m
  3. // LampRibbon
  4. //
  5. // Created by coderYK on 2017/8/7.
  6. // Copyright © 2017年 RD-iOS. All rights reserved.
  7. //
  8. #import "RDSNavController.h"
  9. #import "UIImage+Extension.h"
  10. #import "RDSRootControl.h"
  11. #import "RDSTabbarController.h"
  12. @interface RDSNavController ()
  13. @property (nonatomic, strong) UIBarButtonItem *backItem;
  14. @property (nonatomic, strong) UIBarButtonItem *space;
  15. @property (nonatomic, strong) UIImage *navFillImg; // ** <#注释#>*/
  16. @property (nonatomic, strong) UIImage *navCirImg; // ** <#注释#>*/
  17. @end
  18. @implementation RDSNavController
  19. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
  20. if (self.childViewControllers.count > 0) {
  21. viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
  22. //viewController.navigationItem.leftBarButtonItems = @[self.space, self.backItem];
  23. viewController.navigationItem.leftBarButtonItems = @[self.backItem];
  24. if ([viewController isKindOfClass:[RDSTabbarController class]]) {
  25. if (self.childViewControllers.count < 2) {
  26. viewController.navigationItem.leftBarButtonItems = nil;
  27. viewController.navigationItem.hidesBackButton = YES;
  28. }
  29. }
  30. }
  31. [super pushViewController:viewController animated:animated];
  32. }
  33. - (void)p_popViewController {
  34. [self popViewControllerAnimated:YES];
  35. }
  36. - (UIBarButtonItem *)backItem {
  37. if (_backItem == nil) {
  38. UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  39. [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
  40. backBtn.frame = CGRectMake(0, 0, 30, 30);
  41. [backBtn addTarget:self action:@selector(p_popViewController) forControlEvents:UIControlEventTouchUpInside];
  42. _backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
  43. }
  44. return _backItem;
  45. }
  46. - (UIBarButtonItem *)space {
  47. if (_space == nil) {
  48. _space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
  49. target:nil action:nil];
  50. _space.width = -15;
  51. }
  52. return _space;
  53. }
  54. - (void)viewDidLoad {
  55. [super viewDidLoad];
  56. // 设置导航栏背景图片
  57. // [self.navigationBar setBackgroundImage:[UIImage rds_imageWithColor:RDSNavBarColor] forBarMetrics:UIBarMetricsDefault];
  58. //
  59. // // 去掉导航栏下边的黑边
  60. // [self.navigationBar setShadowImage:[UIImage new]];
  61. //
  62. // // 设置标题字体、颜色
  63. // UIFont *font = [UIFont rds_titiFontStyle:FontStyle_SemiBold size:22];
  64. // [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:font}];
  65. //
  66. [self navBarColor:RDSNavBarColor];
  67. // [self updateNavigationBarColor:RDSNavBarColor];
  68. // [self p_setupNavBar];
  69. }
  70. - (void)navBarColor:(UIColor *)color{
  71. //navigation标题文字颜色
  72. NSDictionary *dic = @{NSForegroundColorAttributeName:RGB(39, 46, 59),
  73. NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
  74. if (@available(iOS 15.0, *)) {
  75. UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
  76. // [barApp configureWithOpaqueBackground];
  77. barApp.backgroundColor = color;
  78. barApp.shadowColor = UIColor.clearColor;
  79. barApp.titleTextAttributes = dic;
  80. self.navigationBar.scrollEdgeAppearance = barApp;
  81. self.navigationBar.standardAppearance = barApp;
  82. }else{
  83. //背景色
  84. self.navigationBar.barTintColor = color;
  85. self.navigationBar.titleTextAttributes = dic;
  86. [self.navigationBar setShadowImage:[UIImage new]];
  87. [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  88. }
  89. //不透明
  90. self.navigationBar.translucent = NO;
  91. //navigation控件颜色
  92. self.navigationBar.tintColor = [UIColor whiteColor];
  93. }
  94. - (void)updateNavigationBarColor:(UIColor *)color {
  95. UINavigationBar *bar = self.navigationBar;// 重置背景和阴影颜色
  96. if (@available(iOS 13.0, *)) {
  97. UINavigationBarAppearance *barAppearance = [UINavigationBarAppearance new];
  98. [barAppearance configureWithOpaqueBackground];
  99. barAppearance.backgroundColor = color; // 设置背景颜色
  100. barAppearance.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor],
  101. NSFontAttributeName : [UIFont systemFontOfSize:24]}; // 设置导航栏字体颜色和大小
  102. barAppearance.shadowColor = [UIColor clearColor]; // 设置导航栏底部的分割线不显示
  103. bar.scrollEdgeAppearance = bar.standardAppearance = barAppearance;
  104. [bar setShadowImage:[UIImage new]];
  105. } else {
  106. // Fallback on earlier versions
  107. }
  108. [bar setBackgroundImage:[UIImage rds_imageWithColor:color] forBarMetrics:UIBarMetricsDefault];
  109. }
  110. - (UIStatusBarStyle)preferredStatusBarStyle {
  111. UIViewController *topVC = self.topViewController;
  112. return [topVC preferredStatusBarStyle];
  113. }
  114. -(void) p_setupNavBar{
  115. UIImage *cirImg = [UIImage imageNamed:@"nav_mine_top"];
  116. // 四个数值对应图片中距离上、左、下、右边界的不拉伸部分的范围宽度
  117. cirImg = [cirImg resizableImageWithCapInsets:UIEdgeInsetsMake(40, 40, 40, 40) resizingMode:UIImageResizingModeStretch];
  118. self.navCirImg = cirImg;
  119. UIImage *fillImg = [UIImage imageNamed:@"nav_top_fill"];
  120. // 四个数值对应图片中距离上、左、下、右边界的不拉伸部分的范围宽度
  121. fillImg = [fillImg resizableImageWithCapInsets:UIEdgeInsetsMake(40, 40, 40, 40) resizingMode:UIImageResizingModeStretch];
  122. self.navFillImg = fillImg;
  123. [self.navigationBar setBackgroundImage:cirImg forBarMetrics:UIBarMetricsDefault];
  124. }
  125. - (void)didReceiveMemoryWarning {
  126. [super didReceiveMemoryWarning];
  127. // Dispose of any resources that can be recreated.
  128. }
  129. @end