UIViewController+SPSBNavigationBar.m 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // UIViewController+SPSBNavigationBar.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/4/21.
  6. //
  7. #import "UIViewController+SPSBNavigationBar.h"
  8. #import <objc/runtime.h>
  9. #import "SPSBUIGeneralHeader.h"
  10. #import "UIViewController+SPSBInitialization.h"
  11. @implementation UIViewController (SPSBNavigationBar)
  12. - (void)createNavigationBar {
  13. if (self.spsb_navigationBar) {
  14. [self.spsb_navigationBar removeFromSuperview];
  15. self.spsb_navigationBar = nil;
  16. }
  17. [self _spsb_setNavigationBar];
  18. [self.view bringSubviewToFront:self.spsb_navigationBar];
  19. }
  20. - (void)_spsb_setNavigationBar {
  21. UIView *navigationBar = self.spsb_navigationBar = UIView.new;
  22. navigationBar.backgroundColor = self.spsb_isLightContent ? spsb_FFFFFF_color(0.f) : spsb_FFFFFF_color(1.f);
  23. [self.view addSubview:navigationBar];
  24. [navigationBar makeConstraints:^(JXHConstraintMaker *make) {
  25. make.top.leading.and.trailing.equalTo(0);
  26. if (@available(iOS 11.0, *)) {
  27. make.bottom.equalTo(self.view.safetop).offset(jxh_navigationViewHeight());
  28. } else {
  29. make.bottom.equalTo(self.view.top).offset(jxh_navigationViewHeight() + jxh_statusBarHeight());
  30. }
  31. }];
  32. UIButton *backButton = self.spsb_barBackButton = [self createBackButton];
  33. [navigationBar addSubview:backButton];
  34. [backButton makeConstraints:^(JXHConstraintMaker *make) {
  35. make.leading.equalTo(16);
  36. make.bottom.equalTo(0);
  37. make.size.equalTo((CGSize){50, jxh_navigationViewHeight()});
  38. }];
  39. UILabel *titleLabel = self.spsb_titleLabel =
  40. [UILabel convenienceWithFont:spsb_mediumFont(16) text:self.title textColor:self.spsb_isLightContent ? spsb_FFFFFF_color(1.f) : spsb_333333_color() textAlignment:NSTextAlignmentCenter];
  41. [navigationBar addSubview:titleLabel];
  42. [titleLabel makeConstraints:^(JXHConstraintMaker *make) {
  43. make.leading.equalTo(80);
  44. make.trailing.equalTo(-80);
  45. make.bottom.equalTo(0);
  46. make.height.equalTo(jxh_navigationViewHeight());
  47. }];
  48. }
  49. - (void)setBackButtonAndTitleColorIsWhite:(bool)isWhite {
  50. if (isWhite) {
  51. self.spsb_titleLabel.textColor = spsb_FFFFFF_color(1.f);
  52. } else {
  53. self.spsb_titleLabel.textColor = spsb_333333_color();
  54. }
  55. if (self.spsb_barBackButton.imageView.image) {
  56. [self.spsb_barBackButton setImage:isWhite ? jxh_getImage(arrow_back_white) : jxh_getImage(arrow_back_black) state:JXHButtonControlStateNormal];
  57. }
  58. if (self.spsb_barBackButton.titleLabel.text) {
  59. [self.spsb_barBackButton setTitleColor:isWhite ? spsb_FFFFFF_color(1.f) : spsb_3296FB_color() state:JXHButtonControlStateNormal];
  60. }
  61. }
  62. - (UIButton *)spsb_barBackButton {
  63. return objc_getAssociatedObject(self, @selector(setSpsb_barBackButton:));
  64. }
  65. - (void)setSpsb_barBackButton:(UIButton *)spsb_barBackButton {
  66. objc_setAssociatedObject(self, _cmd, spsb_barBackButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  67. }
  68. - (UILabel *)spsb_titleLabel {
  69. return objc_getAssociatedObject(self, @selector(setSpsb_titleLabel:));
  70. }
  71. - (void)setSpsb_titleLabel:(UILabel *)spsb_titleLabel {
  72. objc_setAssociatedObject(self, _cmd, spsb_titleLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  73. }
  74. - (UIView *)spsb_navigationBar {
  75. return objc_getAssociatedObject(self, @selector(setSpsb_navigationBar:));
  76. }
  77. - (void)setSpsb_navigationBar:(UIView *)spsb_navigationBar {
  78. objc_setAssociatedObject(self, _cmd, spsb_navigationBar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  79. }
  80. @end