UIViewController+General.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // UIViewController+General.m
  3. // AIIM
  4. //
  5. // Created by qitewei on 2025/5/5.
  6. //
  7. #import "UIViewController+General.h"
  8. #import <objc/runtime.h>
  9. // 关联对象Key
  10. static const char *kBackButtonTitleKey = "kBackButtonTitleKey";
  11. static const char *kBackButtonImageKey = "kBackButtonImageKey";
  12. static const char *kBackButtonColorKey = "kBackButtonColorKey";
  13. static const char *kDisablePopGestureKey = "kDisablePopGestureKey";
  14. @implementation UIViewController (General)
  15. #pragma mark - Navigation Bar 基本配置
  16. - (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated {
  17. [self.navigationController setNavigationBarHidden:hidden animated:animated];
  18. }
  19. - (void)setNavigationBarTransparent:(BOOL)transparent {
  20. if (transparent) {
  21. [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  22. self.navigationController.navigationBar.shadowImage = [UIImage new];
  23. self.navigationController.navigationBar.translucent = YES;
  24. } else {
  25. [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
  26. self.navigationController.navigationBar.shadowImage = nil;
  27. self.navigationController.navigationBar.translucent = NO;
  28. }
  29. }
  30. - (void)setNavigationBarBackgroundColor:(UIColor *)color {
  31. if (@available(iOS 15.0, *)) {
  32. UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
  33. [appearance configureWithOpaqueBackground];
  34. appearance.backgroundColor = color;
  35. appearance.shadowColor = [UIColor clearColor];
  36. self.navigationController.navigationBar.standardAppearance = appearance;
  37. self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
  38. } else {
  39. self.navigationController.navigationBar.barTintColor = color;
  40. self.navigationController.navigationBar.translucent = NO;
  41. }
  42. }
  43. - (void)setNavigationTitle:(NSString *)title {
  44. self.navigationItem.title = title;
  45. }
  46. - (void)setNavigationTitleColor:(UIColor *)color font:(UIFont *)font {
  47. NSDictionary *attributes = @{
  48. NSForegroundColorAttributeName: color,
  49. NSFontAttributeName: font
  50. };
  51. if (@available(iOS 15.0, *)) {
  52. UINavigationBarAppearance *appearance = self.navigationController.navigationBar.standardAppearance ?: [[UINavigationBarAppearance alloc] init];
  53. appearance.titleTextAttributes = attributes;
  54. self.navigationController.navigationBar.standardAppearance = appearance;
  55. self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
  56. } else {
  57. [self.navigationController.navigationBar setTitleTextAttributes:attributes];
  58. }
  59. }
  60. #pragma mark - 返回按钮配置
  61. - (void)setBackButtonTitle:(NSString *)title {
  62. objc_setAssociatedObject(self, kBackButtonTitleKey, title, OBJC_ASSOCIATION_COPY_NONATOMIC);
  63. [self configureBackButton];
  64. }
  65. - (void)setBackButtonImage:(UIImage *)image {
  66. objc_setAssociatedObject(self, kBackButtonImageKey, image, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  67. [self configureBackButton];
  68. }
  69. - (void)setBackButtonColor:(UIColor *)color {
  70. objc_setAssociatedObject(self, kBackButtonColorKey, color, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  71. [self configureBackButton];
  72. }
  73. - (void)disableInteractivePopGesture:(BOOL)disable {
  74. objc_setAssociatedObject(self, kDisablePopGestureKey, @(disable), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  75. self.navigationController.interactivePopGestureRecognizer.enabled = !disable;
  76. }
  77. - (void)configureBackButton {
  78. NSString *title = objc_getAssociatedObject(self, kBackButtonTitleKey) ?: @"";
  79. UIImage *image = objc_getAssociatedObject(self, kBackButtonImageKey) ?: [UIImage imageNamed:@"fanhui"];
  80. UIColor *color = objc_getAssociatedObject(self, kBackButtonColorKey) ?: [UIColor whiteColor];
  81. UIBarButtonItem *backItem;
  82. if (image) {
  83. backItem = [[UIBarButtonItem alloc] initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  84. style:UIBarButtonItemStylePlain
  85. target:self
  86. action:@selector(popViewController)];
  87. } else {
  88. backItem = [[UIBarButtonItem alloc] initWithTitle:title
  89. style:UIBarButtonItemStylePlain
  90. target:self
  91. action:@selector(popViewController)];
  92. }
  93. [backItem setTitleTextAttributes:@{NSForegroundColorAttributeName: color} forState:UIControlStateNormal];
  94. self.navigationItem.backBarButtonItem = nil;
  95. self.navigationItem.leftBarButtonItem = backItem;
  96. }
  97. #pragma mark - 添加自定义按钮
  98. - (void)addBarButtonWithTitle:(NSString *)title position:(BarButtonItemPosition)position action:(SEL)action {
  99. UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:title
  100. style:UIBarButtonItemStylePlain
  101. target:self
  102. action:action];
  103. [self addBarButtonItem:button atPosition:position];
  104. }
  105. - (void)addBarButtonWithImage:(UIImage *)image position:(BarButtonItemPosition)position action:(SEL)action {
  106. UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  107. style:UIBarButtonItemStylePlain
  108. target:self
  109. action:action];
  110. [self addBarButtonItem:button atPosition:position];
  111. }
  112. - (void)addBarButtonWithCustomView:(UIView *)view position:(BarButtonItemPosition)position {
  113. UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:view];
  114. [self addBarButtonItem:button atPosition:position];
  115. }
  116. - (void)addBarButtonItem:(UIBarButtonItem *)item atPosition:(BarButtonItemPosition)position {
  117. if (position == BarButtonItemPositionLeft) {
  118. NSMutableArray *leftItems = [NSMutableArray arrayWithArray:self.navigationItem.leftBarButtonItems ?: @[]];
  119. [leftItems addObject:item];
  120. self.navigationItem.leftBarButtonItems = leftItems;
  121. } else {
  122. NSMutableArray *rightItems = [NSMutableArray arrayWithArray:self.navigationItem.rightBarButtonItems ?: @[]];
  123. [rightItems addObject:item];
  124. self.navigationItem.rightBarButtonItems = rightItems;
  125. }
  126. }
  127. - (void)removeBarButtonAtPosition:(BarButtonItemPosition)position {
  128. if (position == BarButtonItemPositionLeft) {
  129. self.navigationItem.leftBarButtonItems = nil;
  130. } else {
  131. self.navigationItem.rightBarButtonItems = nil;
  132. }
  133. }
  134. #pragma mark - 工具方法
  135. - (void)popViewController {
  136. [self.navigationController popViewControllerAnimated:YES];
  137. }
  138. - (void)popToRootViewController {
  139. [self.navigationController popToRootViewControllerAnimated:YES];
  140. }
  141. @end