TDNavigationBar.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.size.mas_equalTo(CGSizeMake(50, 44));
  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.width.mas_lessThanOrEqualTo(150);
  40. }];
  41. _backButton2.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
  42. _rightButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
  43. [_lineVi mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.left.bottom.right.equalTo(self);
  45. make.height.mas_equalTo(0.5f);
  46. }];
  47. _lineVi.hidden = YES;
  48. }
  49. return self;
  50. }
  51. - (void)setTitle:(NSString *)titleStr {
  52. _titleLabel.text = titleStr;
  53. // CGSize titleSize = [_titleLabel sizeThatFits:CGSizeZero];
  54. // [_titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  55. // make.size.mas_equalTo(titleSize);
  56. // }];
  57. }
  58. - (void)setNavigationTintType:(NavigationTintType)type {
  59. if (type == NavigationTintTypeBlack) {
  60. [self.backButton setImage:IMG(@"back_black_icon") forState:UIControlStateNormal];
  61. [self.backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  62. [self.rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  63. self.backgroundColor = [UIColor whiteColor];
  64. self.lineVi.backgroundColor = [UIColor whiteColor];
  65. } else if (type == NavigationTintTypeWhite) {
  66. [self.backButton setImage:IMG(@"icon_back_white") forState:UIControlStateNormal];
  67. [self.backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  68. [self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  69. self.backgroundColor = [UIColor blackColor];
  70. self.lineVi.backgroundColor = [UIColor blackColor];
  71. }
  72. }
  73. - (void)setBackButtonTitle:(NSString *)backStr {
  74. [_backButton setTitle:backStr forState:UIControlStateNormal];
  75. CGSize titleSize = [NSString calculateSizeWithString:backStr fontSize:15.f];
  76. [_backButton mas_updateConstraints:^(MASConstraintMaker *make) {
  77. make.size.mas_equalTo(CGSizeMake(10+8+titleSize.width, 20));
  78. }];
  79. }
  80. - (void)setRightButtonTitle:(NSString *)rightStr {
  81. [self addSubview:self.rightButton];
  82. [_rightButton setTitle:rightStr forState:UIControlStateNormal];
  83. [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.right.mas_equalTo(self.mas_right);
  85. make.centerY.equalTo(self);
  86. }];
  87. }
  88. - (void)backButtonTouch {
  89. if ([self.delegate respondsToSelector:@selector(backButtonAction)]) {
  90. [self.delegate backButtonAction];
  91. return;
  92. }
  93. [self.viewController.navigationController popViewControllerAnimated:YES];
  94. }
  95. - (void)backButtonTouch2:(TDButton *)sender {
  96. if ([self.delegate respondsToSelector:@selector(backButtonAction2:)]) {
  97. [self.delegate backButtonAction2:sender];
  98. return;
  99. }
  100. [self.viewController.navigationController popViewControllerAnimated:YES];
  101. }
  102. - (void)rightButtonTouch:(TDButton *)sender {
  103. if ([self.delegate respondsToSelector:@selector(rightButtonAction:)]) {
  104. [self.delegate rightButtonAction:sender];
  105. }
  106. }
  107. #pragma mark - setter
  108. - (TDButton *)backButton {
  109. if (!_backButton) {
  110. _backButton = [[TDButton alloc] init];
  111. [_backButton setImage:IMG(@"back_black_icon") forState:UIControlStateNormal];
  112. [[_backButton titleLabel] setFont:[UIFont systemFontOfSize:15.f]];
  113. [_backButton addTarget:self action:@selector(backButtonTouch) forControlEvents:UIControlEventTouchUpInside];
  114. [_backButton setCurrentButtonHotSize:CGSizeZero];
  115. }
  116. return _backButton;
  117. }
  118. - (TDButton *)backButton2 {
  119. if (!_backButton2) {
  120. _backButton2 = [[TDButton alloc] init];
  121. _backButton2.hidden = YES;
  122. [_backButton2 setImage:IMG(@"menu_black_icon") forState:UIControlStateNormal];
  123. [[_backButton2 titleLabel] setFont:[UIFont systemFontOfSize:15.f]];
  124. [_backButton2 addTarget:self action:@selector(backButtonTouch2:) forControlEvents:UIControlEventTouchUpInside];
  125. [_backButton2 setCurrentButtonHotSize:CGSizeZero];
  126. }
  127. return _backButton2;
  128. }
  129. - (UILabel *)titleLabel {
  130. if (!_titleLabel) {
  131. _titleLabel = [[UILabel alloc] init];
  132. _titleLabel.font = [UIFont systemFontOfSize:16.f];
  133. _titleLabel.textColor = UIColorHex(#333333);
  134. _titleLabel.textAlignment = NSTextAlignmentCenter;
  135. }
  136. return _titleLabel;
  137. }
  138. - (UIView *)lineVi {
  139. if (!_lineVi) {
  140. _lineVi = [UIView new];
  141. _lineVi.backgroundColor = UIColorHex(eeeeee);
  142. }
  143. return _lineVi;
  144. }
  145. - (TDButton *)rightButton {
  146. if (!_rightButton) {
  147. _rightButton = [[TDButton alloc] init];
  148. [_rightButton addTarget:self action:@selector(rightButtonTouch:) forControlEvents:UIControlEventTouchUpInside];
  149. [_rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  150. [[_rightButton titleLabel] setFont:[UIFont systemFontOfSize:15.f]];
  151. [_rightButton setCurrentButtonHotSize:CGSizeZero];
  152. _rightButton.hidden = YES;
  153. }
  154. return _rightButton;
  155. }
  156. @end