dkahgld

ZBNavigationBar.m 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // ZBNavigationBar.m
  3. // ZBProject
  4. //
  5. // Created by 学丽 on 2019/3/26.
  6. // Copyright © 2019 ZB. All rights reserved.
  7. //
  8. #import "ZBNavigationBar.h"
  9. static const float NavigationBar_Title_Font_Size = 16.0f;
  10. static NSString *const Font_Default_Medium = @"PingFangSC-Medium";
  11. @interface ZBNavigationBar(){
  12. NSArray<UIButton*> *_leftButtons;
  13. BOOL _isCustomTitleView;
  14. UIView* _titleView;
  15. UILabel* _titleLabel;
  16. NSArray<UIButton*>* _rightButtons;
  17. UIImageView *_bgImageV;
  18. UIView *_bottomLine;
  19. }
  20. @end
  21. @implementation ZBNavigationBar
  22. - (instancetype)initWithFrame:(CGRect)frame
  23. {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. _isCustomTitleView = NO;
  27. [self drawView];
  28. }
  29. return self;
  30. }
  31. - (void)drawView
  32. {
  33. _bgImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
  34. // [_bgImageV setImage:bgImg];
  35. _bgImageV.hidden = YES;
  36. [self addSubview:_bgImageV];
  37. self.backgroundColor = [UIColor whiteColor];
  38. _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1)];
  39. _bottomLine.backgroundColor = [UIColor YHColorWithHex:0xdddddd];
  40. [self addSubview:_bottomLine];
  41. _bottomLine.hidden = YES;
  42. }
  43. #pragma mark - Public Method
  44. - (void)setBackButtonWithTarget:(id)target selector:(SEL)selector
  45. {
  46. UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
  47. button.bounds = CGRectMake(0, 0, 44.0f, 44.0f);
  48. button.frame = CGRectMake(0, KStatusBarHeight, 44.0f, 44.0f);
  49. // [button setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] frame:button.bounds] forState:UIControlStateNormal];
  50. // [button setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] frame:button.bounds] forState:UIControlStateHighlighted];
  51. // [button setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] frame:button.bounds] forState:UIControlStateDisabled];
  52. [button setImage:[UIImage imageNamed:@"back" ] forState:UIControlStateNormal];
  53. [button setImage:[UIImage imageNamed:@"back" ] forState:UIControlStateHighlighted];
  54. [self setCustomButtonWithButton:button target:target selector:selector];
  55. }
  56. - (void)setCustomButtonWithButton:(UIButton *)button target:(id)target selector:(SEL)selector
  57. {
  58. if (!button) {
  59. _leftButtons = nil;
  60. [self layoutElement];
  61. return;
  62. }
  63. _leftButtons = @[button];
  64. [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
  65. [self setCustomLeftButtons:_leftButtons];
  66. }
  67. - (void)setCustomLeftButtons:(NSArray<UIButton*>*)buttons
  68. {
  69. for (UIButton *button in _leftButtons) {
  70. [button removeFromSuperview];
  71. }
  72. _leftButtons = buttons;
  73. [self layoutElement];
  74. }
  75. - (void)setCustomRightButtons:(NSArray<UIButton *> *)buttons
  76. {
  77. for (UIButton* button in _rightButtons) {
  78. [button removeFromSuperview];
  79. }
  80. _rightButtons = buttons;
  81. for (UIButton* button in _rightButtons) {
  82. [self addSubview:button];
  83. }
  84. [self layoutElement];
  85. }
  86. - (void)setNavTitle:(NSString *)title
  87. {
  88. [_titleView removeFromSuperview];
  89. _titleView = nil;
  90. _titleView = [[UIView alloc] init];
  91. _titleView.backgroundColor = [UIColor clearColor];
  92. UILabel* label = [[UILabel alloc] init];
  93. label.font = [UIFont fontWithName:Font_Default_Medium size:NavigationBar_Title_Font_Size];
  94. label.textAlignment = NSTextAlignmentCenter;
  95. label.textColor = [UIColor YHColorWithHex:0x333333];
  96. label.userInteractionEnabled = YES;
  97. label.text = title;
  98. _titleLabel = label;
  99. [_titleView addSubview:label];
  100. [self addSubview:_titleView];
  101. _isCustomTitleView = NO;
  102. [self layoutElement];
  103. }
  104. - (void)setCustomTitleView:(UIView *)titleView
  105. {
  106. [_titleView removeFromSuperview];
  107. _titleView = titleView;
  108. [self addSubview:_titleView];
  109. _isCustomTitleView = YES;
  110. [self layoutElement];
  111. }
  112. - (void)setYHNavigationBarStyle:(ZBNavigationBarStyle)barStyle
  113. {
  114. switch (barStyle) {
  115. case YHNavigationBarStyleNormal:
  116. {
  117. self.backgroundColor = [UIColor gradientWidthFromColor:[UIColor YHColorWithHex:0xFF9600] toColor:[UIColor YHColorWithHex:0xFF7200] withWidth:SCREEN_WIDTH];
  118. _bgImageV.hidden = YES;
  119. _titleLabel.textColor=[UIColor whiteColor];
  120. }
  121. break;
  122. case YHNavigationBarStyleTransparent:
  123. {
  124. self.backgroundColor = [UIColor clearColor];
  125. _bgImageV.hidden = NO;
  126. _titleLabel.textColor=[UIColor YHColorWithHex:0x333333];
  127. }
  128. break;
  129. case YHNavigationBarStyleWhite:
  130. {
  131. self.backgroundColor = [UIColor YHColorWithHex:0xffffff alpha:1.0f];
  132. _bgImageV.hidden = YES;
  133. _titleLabel.textColor=[UIColor YHColorWithHex:0x333333];
  134. }
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. - (void)setNavightionBarBackgroundColor:(UIColor *)color {
  141. self.backgroundColor = color;
  142. _bgImageV.hidden = YES;
  143. }
  144. #pragma mark - Private Method
  145. - (void)layoutElement
  146. {
  147. CGFloat x = Fitsize(15);
  148. for (NSInteger index = 0; index < _leftButtons.count; index ++) {
  149. UIButton* button = _leftButtons[index];
  150. button.frame = CGRectMake(x, KStatusBarHeight+22-button.height/2, button.width, button.height);
  151. [self addSubview:button];
  152. x += button.width;
  153. }
  154. x = self.width;
  155. for (NSInteger index = _rightButtons.count - 1; index >= 0; index --) {
  156. UIButton* button = _rightButtons[index];
  157. x = x - button.width - FITSIZE(15);
  158. button.frame = CGRectMake(x, KStatusBarHeight+22-button.height/2, button.width, button.height);
  159. }
  160. // 两边都有按钮
  161. if (_leftButtons.count > 0 && _rightButtons.count > 0) {
  162. _titleView.frame = CGRectMake(_leftButtons[_leftButtons.count - 1].right, KStatusBarHeight, _rightButtons[0].x - _leftButtons[_leftButtons.count - 1].right, 44.0f);
  163. }
  164. // 只有左侧有按钮
  165. else if (_leftButtons.count > 0 && (_rightButtons.count == 0 || !_rightButtons ))
  166. {
  167. _titleView.frame = CGRectMake(_leftButtons[_leftButtons.count - 1].right, KStatusBarHeight,self.width - _leftButtons[_leftButtons.count - 1].width * 2, 44.0f);
  168. }
  169. // 只有右侧有按钮
  170. else if ((!_leftButtons || _leftButtons.count == 0) && _rightButtons.count > 0)
  171. {
  172. CGFloat leftWidth = self.width - _rightButtons[0].x;
  173. _titleView.frame = CGRectMake(leftWidth, KStatusBarHeight, self.width - leftWidth * 2, 44.0f);
  174. }
  175. // 两边都没有按钮
  176. else
  177. {
  178. _titleView.frame = CGRectMake(0, KStatusBarHeight, self.width, 44.0f);
  179. }
  180. if (!_isCustomTitleView) {
  181. CGFloat titleViewDistanceFromLeft = _titleView.x;
  182. CGFloat titleViewDistanceFromRight = self.width - _titleView.right;
  183. CGFloat titleLabelShouldWidth = 0;
  184. if (titleViewDistanceFromLeft > titleViewDistanceFromRight) {
  185. titleLabelShouldWidth = (self.width / 2.0f - titleViewDistanceFromLeft) * 2;
  186. }
  187. else
  188. {
  189. titleLabelShouldWidth = (self.width / 2.0f - titleViewDistanceFromRight) * 2;
  190. }
  191. _titleLabel.width = titleLabelShouldWidth;
  192. _titleLabel.x = 0;
  193. _titleLabel.y = 0;
  194. CGFloat titleLabelShouldXOnSelf = (self.width - titleLabelShouldWidth) / 2.0f;
  195. CGFloat titleLabelX = [_titleLabel convertRect:_titleLabel.bounds toView:self].origin.x;
  196. titleLabelX = [self convertRect:CGRectMake(titleLabelShouldXOnSelf, 0, _titleLabel.width, _titleLabel.height) toView:_titleView].origin.x;
  197. _titleLabel.x = titleLabelX;
  198. _titleLabel.height = _titleView.height;
  199. }
  200. }
  201. #pragma public ----------
  202. - (void)setShowNavigationBarBottomLine:(BOOL)showNavigationBarBottomLine {
  203. _bottomLine.hidden = !showNavigationBarBottomLine;
  204. }
  205. - (UILabel *)navTitleLabel {
  206. return _titleLabel;
  207. }
  208. @end