TDNavigationBar.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // TDNavigationBar.m
  3. // TheoryNetwork
  4. //
  5. // Created by tederen on 2019/9/26.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "TDNavigationBar.h"
  9. @interface TDNavigationBar ()
  10. @end
  11. @implementation TDNavigationBar
  12. //@synthesize backButton2;
  13. - (instancetype)initNavigationBar
  14. {
  15. self = [super init];
  16. if (self) {
  17. self.frame = CGRectMake(0,(IS_IPHONEX?40:20), kGXScreenWidth, 44);
  18. [self addSubview:self.backButton];
  19. [self addSubview:self.backButton2];
  20. [self addSubview:self.titleLabel];
  21. [self addSubview:self.rightButton];
  22. [self addSubview:self.lineVi];
  23. [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
  24. make.left.equalTo(self).offset(0);
  25. make.centerY.equalTo(self);
  26. make.size.mas_equalTo(CGSizeMake(50, 44));
  27. }];
  28. [_backButton2 mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.left.mas_equalTo(self.backButton.mas_right);
  30. make.centerY.equalTo(self.backButton);
  31. make.width.mas_equalTo(19);
  32. }];
  33. [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.right.mas_equalTo(self.mas_right).offset(-15);
  35. make.centerY.equalTo(self);
  36. }];
  37. [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  38. make.center.equalTo(self);
  39. make.left.mas_equalTo(self.backButton2.mas_right).offset(5);
  40. make.right.mas_equalTo(self.rightButton.mas_left).offset(-5);
  41. }];
  42. _backButton2.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
  43. _rightButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
  44. [_lineVi mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.left.bottom.right.equalTo(self);
  46. make.height.mas_equalTo(0.5f);
  47. }];
  48. _lineVi.hidden = YES;
  49. }
  50. return self;
  51. }
  52. - (void)setTitle:(NSString *)titleStr {
  53. _titleLabel.text = titleStr;
  54. // CGSize titleSize = [_titleLabel sizeThatFits:CGSizeZero];
  55. // [_titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  56. // make.size.mas_equalTo(titleSize);
  57. // }];
  58. }
  59. - (void)setNavigationTintType:(NavigationTintType)type {
  60. if (type == NavigationTintTypeBlack) {
  61. [self.backButton setImage:IMG(@"back_black_icon") forState:UIControlStateNormal];
  62. [self.backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  63. [self.rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  64. self.backgroundColor = [UIColor whiteColor];
  65. self.lineVi.backgroundColor = [UIColor whiteColor];
  66. } else if (type == NavigationTintTypeWhite) {
  67. [self.backButton setImage:IMG(@"icon_back_white") forState:UIControlStateNormal];
  68. [self.backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  69. [self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  70. self.backgroundColor = [UIColor blackColor];
  71. self.lineVi.backgroundColor = [UIColor blackColor];
  72. }
  73. }
  74. - (void)setBackButtonTitle:(NSString *)backStr {
  75. [_backButton setTitle:backStr forState:UIControlStateNormal];
  76. CGSize titleSize = [NSString calculateSizeWithString:backStr fontSize:15.f];
  77. [_backButton mas_updateConstraints:^(MASConstraintMaker *make) {
  78. make.size.mas_equalTo(CGSizeMake(10+8+titleSize.width, 20));
  79. }];
  80. }
  81. - (void)setRightButtonTitle:(NSString *)rightStr {
  82. [self addSubview:self.rightButton];
  83. [_rightButton setTitle:rightStr forState:UIControlStateNormal];
  84. [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
  85. make.right.mas_equalTo(self.mas_right);
  86. make.centerY.equalTo(self);
  87. }];
  88. }
  89. - (void)backButtonTouch {
  90. if ([self.delegate respondsToSelector:@selector(backButtonAction)]) {
  91. [self.delegate backButtonAction];
  92. return;
  93. }
  94. [self.viewController.navigationController popViewControllerAnimated:YES];
  95. }
  96. - (void)backButtonTouch2:(TDButton *)sender {
  97. if ([self.delegate respondsToSelector:@selector(backButtonAction2:)]) {
  98. [self.delegate backButtonAction2:sender];
  99. return;
  100. }
  101. [self.viewController.navigationController popViewControllerAnimated:YES];
  102. }
  103. - (void)rightButtonTouch:(TDButton *)sender {
  104. if ([self.delegate respondsToSelector:@selector(rightButtonAction:)]) {
  105. [self.delegate rightButtonAction:sender];
  106. }
  107. }
  108. #pragma mark - setter
  109. - (TDButton *)backButton {
  110. if (!_backButton) {
  111. _backButton = [[TDButton alloc] init];
  112. [_backButton setImage:IMG(@"back_black_icon") forState:UIControlStateNormal];
  113. [[_backButton titleLabel] setFont:[UIFont systemFontOfSize:15.f]];
  114. [_backButton addTarget:self action:@selector(backButtonTouch) forControlEvents:UIControlEventTouchUpInside];
  115. [_backButton setCurrentButtonHotSize:CGSizeZero];
  116. }
  117. return _backButton;
  118. }
  119. - (TDButton *)backButton2 {
  120. if (!_backButton2) {
  121. _backButton2 = [[TDButton alloc] init];
  122. _backButton2.hidden = YES;
  123. [_backButton2 setImage:IMG(@"menu_black_icon") forState:UIControlStateNormal];
  124. [[_backButton2 titleLabel] setFont:[UIFont systemFontOfSize:15.f]];
  125. [_backButton2 addTarget:self action:@selector(backButtonTouch2:) forControlEvents:UIControlEventTouchUpInside];
  126. [_backButton2 setCurrentButtonHotSize:CGSizeZero];
  127. }
  128. return _backButton2;
  129. }
  130. - (UILabel *)titleLabel {
  131. if (!_titleLabel) {
  132. _titleLabel = [[UILabel alloc] init];
  133. _titleLabel.font = [UIFont systemFontOfSize:17.f];
  134. _titleLabel.textColor = UIColorHex(#333333);
  135. _titleLabel.textAlignment = NSTextAlignmentCenter;
  136. }
  137. return _titleLabel;
  138. }
  139. - (UIView *)lineVi {
  140. if (!_lineVi) {
  141. _lineVi = [UIView new];
  142. _lineVi.backgroundColor = UIColorHex(eeeeee);
  143. }
  144. return _lineVi;
  145. }
  146. - (TDButton *)rightButton {
  147. if (!_rightButton) {
  148. _rightButton = [[TDButton alloc] init];
  149. [_rightButton addTarget:self action:@selector(rightButtonTouch:) forControlEvents:UIControlEventTouchUpInside];
  150. [_rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  151. [[_rightButton titleLabel] setFont:[UIFont systemFontOfSize:15.f]];
  152. [_rightButton setCurrentButtonHotSize:CGSizeZero];
  153. _rightButton.hidden = YES;
  154. }
  155. return _rightButton;
  156. }
  157. @end