UIViewController+SPSBInitialization.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // UIViewController+SPSBInitialization.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/4/19.
  6. //
  7. #import "UIViewController+SPSBInitialization.h"
  8. #import <objc/runtime.h>
  9. #import "SPSBUIGeneralHeader.h"
  10. #import <JXHGeneralTools.h>
  11. #import <UMCommon/MobClick.h>
  12. @implementation UIViewController (SPSBInitialization)
  13. + (void)load {
  14. jxh_sizzleMethod(self, @selector(viewDidLoad), @selector(SPSBSwizzled_viewDidLoad));
  15. jxh_sizzleMethod(self, @selector(viewWillAppear:), @selector(SPSBSwizzled_viewWillAppear:));
  16. jxh_sizzleMethod(self, @selector(viewWillDisappear:), @selector(SPSBSwizzled_viewWillDisappear:));
  17. jxh_sizzleMethod(self, @selector(viewDidAppear:), @selector(SPSBSwizzled_viewDidAppear:));
  18. jxh_sizzleMethod(self, @selector(viewDidDisappear:), @selector(SPSBSwizzled_viewDidDisappear:));
  19. }
  20. - (void)SPSBSwizzled_viewDidLoad {
  21. if ([NSStringFromClass([self class]) hasPrefix:@"SPSB"]) {
  22. self.automaticallyAdjustsScrollViewInsets = false;
  23. self.view.backgroundColor = spsb_FFFFFF_color(1.f);
  24. if (!self.parentViewController || [self.parentViewController isKindOfClass:[UINavigationController class]]) {
  25. UIButton *backButton = self.spsb_backButton = [self createBackButton];
  26. backButton.frame = CGRectMake(0, 0, 50, jxh_navigationViewHeight());
  27. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
  28. self.navigationItem.leftBarButtonItem = backItem;
  29. //启动返回手势
  30. self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
  31. }
  32. }
  33. [self SPSBSwizzled_viewDidLoad];
  34. }
  35. - (void)SPSBSwizzled_viewWillAppear:(BOOL)animated {
  36. if ([NSStringFromClass([self class]) hasPrefix:@"SPSB"]) {
  37. if (!self.parentViewController || [self.parentViewController isKindOfClass:[UINavigationController class]]) {
  38. if (![self isKindOfClass:[UINavigationController class]] && self.modalPresentationStyle != UIModalPresentationCustom) {
  39. [MobClick beginLogPageView:NSStringFromClass([self class])];
  40. if (self.navigationController) {
  41. [self.navigationController setNavigationBarHidden:self.spsb_isHiddenNavigationBar animated:true];
  42. }
  43. }
  44. }
  45. }
  46. [self SPSBSwizzled_viewWillAppear:animated];
  47. }
  48. - (void)SPSBSwizzled_viewWillDisappear:(BOOL)animated {
  49. if ([NSStringFromClass([self class]) hasPrefix:@"SPSB"]) {
  50. if (!self.parentViewController || [self.parentViewController isKindOfClass:[UINavigationController class]]) {
  51. if (![self isKindOfClass:[UINavigationController class]] && self.modalPresentationStyle != UIModalPresentationCustom) {
  52. [MobClick endLogPageView:NSStringFromClass([self class])];
  53. }
  54. }
  55. }
  56. [self SPSBSwizzled_viewWillDisappear:animated];
  57. }
  58. - (void)SPSBSwizzled_viewDidAppear:(BOOL)animated {
  59. if ([NSStringFromClass([self class]) hasPrefix:@"SPSB"]) {
  60. if (!self.parentViewController || [self.parentViewController isKindOfClass:[UINavigationController class]]) {
  61. if (self.navigationController && self.navigationController.viewControllers.count == 1) {
  62. //取消滑动手势返回
  63. self.navigationController.interactivePopGestureRecognizer.enabled = false;
  64. }
  65. }
  66. }
  67. [self SPSBSwizzled_viewDidAppear:animated];
  68. }
  69. - (void)SPSBSwizzled_viewDidDisappear:(BOOL)animated {
  70. if ([NSStringFromClass([self class]) hasPrefix:@"SPSB"]) {
  71. if (!self.parentViewController || [self.parentViewController isKindOfClass:[UINavigationController class]]) {
  72. //启动滑动手势返回
  73. self.navigationController.interactivePopGestureRecognizer.enabled = true;
  74. }
  75. }
  76. [self SPSBSwizzled_viewDidDisappear:animated];
  77. }
  78. - (void)cancelAction {
  79. jxh_endEdit();
  80. [self dismissViewControllerAnimated:true completion:^{
  81. }];
  82. }
  83. - (void)backAction {
  84. jxh_endEdit();
  85. [self.navigationController popViewControllerAnimated:true];
  86. }
  87. - (UIButton *)createBackButton {
  88. UIButton *backButton;
  89. if (!self.navigationController || self.navigationController.viewControllers.count == 1) {
  90. backButton = [UIButton convenienceWithFont:spsb_font(15) target:self action:@selector(cancelAction)];
  91. [backButton setTitle:@"取消" titleColor:self.spsb_isLightContent ? spsb_FFFFFF_color(1.f) : spsb_3296FB_color() state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted];
  92. } else {
  93. backButton = [UIButton convenienceWithFont:spsb_font(15) target:self action:@selector(backAction)];
  94. [backButton setTitle:@"返回" titleColor:self.spsb_isLightContent ? spsb_FFFFFF_color(1.f) : spsb_3296FB_color() image:self.spsb_isLightContent ? jxh_getImage(arrow_back_white) : jxh_getImage(arrow_back_blue) backgroundImage:nil state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted];
  95. [backButton setImageEdgeInsets:UIEdgeInsetsMake(0, -2, 0, 2)];
  96. }
  97. [backButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
  98. return backButton;
  99. }
  100. - (UIButton *)spsb_backButton {
  101. UIButton *object = objc_getAssociatedObject(self, @selector(setSpsb_backButton:));
  102. return object;
  103. }
  104. - (void)setSpsb_backButton:(UIButton *)spsb_backButton {
  105. objc_setAssociatedObject(self, _cmd, spsb_backButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  106. }
  107. - (UIStatusBarStyle)spsb_statusBarStyle {
  108. return [objc_getAssociatedObject(self, @selector(setSpsb_statusBarStyle:)) integerValue];
  109. }
  110. - (void)setSpsb_statusBarStyle:(UIStatusBarStyle)spsb_statusBarStyle {
  111. objc_setAssociatedObject(self, _cmd, @(spsb_statusBarStyle), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  112. }
  113. - (bool)spsb_isLightContent {
  114. return [objc_getAssociatedObject(self, @selector(setSpsb_isLightContent:)) boolValue];
  115. }
  116. - (void)setSpsb_isLightContent:(bool)spsb_isLightContent {
  117. if (spsb_isLightContent) {
  118. self.spsb_statusBarStyle = UIStatusBarStyleLightContent;
  119. } else {
  120. self.spsb_statusBarStyle = UIStatusBarStyleDefault;
  121. }
  122. [self setNeedsStatusBarAppearanceUpdate];
  123. objc_setAssociatedObject(self, _cmd, @(spsb_isLightContent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  124. }
  125. - (bool)spsb_isHiddenNavigationBar {
  126. return [objc_getAssociatedObject(self, @selector(setSpsb_isHiddenNavigationBar:)) boolValue];
  127. }
  128. - (void)setSpsb_isHiddenNavigationBar:(bool)spsb_isHiddenNavigationBar {
  129. objc_setAssociatedObject(self, _cmd, @(spsb_isHiddenNavigationBar), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  130. }
  131. @end