// // KBNavigationBar.m // YouHuiProject // // Created by 小花 on 2018/1/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBNavigationBar.h" static const float NavigationBar_Title_Font_Size = 16.0f; static NSString *const Font_Default_Medium = @"PingFangSC-Medium"; @interface KBNavigationBar(){ NSArray *_leftButtons; BOOL _isCustomTitleView; UIView* _titleView; UIView* _titleLabel; NSArray* _rightButtons; UIImageView *_bgImageV; UIView *_bottomLine; } @end @implementation KBNavigationBar - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _isCustomTitleView = NO; [self drawView]; } return self; } - (void)drawView { _bgImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)]; // [_bgImageV setImage:bgImg]; _bgImageV.hidden = YES; [self addSubview:_bgImageV]; self.backgroundColor = [UIColor whiteColor]; _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1)]; _bottomLine.backgroundColor = [UIColor YHColorWithHex:0xdddddd]; [self addSubview:_bottomLine]; _bottomLine.hidden = YES; } #pragma mark - Public Method - (void)setBackButtonWithTarget:(id)target selector:(SEL)selector { UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; button.bounds = CGRectMake(0, 0, 44.0f, 44.0f); button.frame = CGRectMake(0, KStatusBarHeight, 44.0f, 44.0f); // [button setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] frame:button.bounds] forState:UIControlStateNormal]; // [button setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] frame:button.bounds] forState:UIControlStateHighlighted]; // [button setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] frame:button.bounds] forState:UIControlStateDisabled]; [button setImage:[UIImage imageNamed:@"back" ] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:@"back" ] forState:UIControlStateHighlighted]; [self setCustomButtonWithButton:button target:target selector:selector]; } - (void)setCustomButtonWithButton:(UIButton *)button target:(id)target selector:(SEL)selector { if (!button) { _leftButtons = nil; [self layoutElement]; return; } _leftButtons = @[button]; [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside]; [self setCustomLeftButtons:_leftButtons]; } - (void)setCustomLeftButtons:(NSArray*)buttons { for (UIButton *button in _leftButtons) { [button removeFromSuperview]; } _leftButtons = buttons; [self layoutElement]; } - (void)setCustomRightButtons:(NSArray *)buttons { for (UIButton* button in _rightButtons) { [button removeFromSuperview]; } _rightButtons = buttons; for (UIButton* button in _rightButtons) { [self addSubview:button]; } [self layoutElement]; } - (void)setNavTitle:(NSString *)title { [_titleView removeFromSuperview]; _titleView = nil; _titleView = [[UIView alloc] init]; _titleView.backgroundColor = [UIColor clearColor]; UILabel* label = [[UILabel alloc] init]; label.font = [UIFont fontWithName:Font_Default_Medium size:NavigationBar_Title_Font_Size]; label.textAlignment = NSTextAlignmentCenter; label.textColor = [UIColor YHColorWithHex:0x333333]; label.text = title; label.userInteractionEnabled = YES; _titleLabel = label; [_titleView addSubview:label]; [self addSubview:_titleView]; _isCustomTitleView = NO; [self layoutElement]; } - (void)setCustomTitleView:(UIView *)titleView { [_titleView removeFromSuperview]; _titleView = titleView; [self addSubview:_titleView]; _isCustomTitleView = YES; [self layoutElement]; } - (void)setYHNavigationBarStyle:(YHNavigationBarStyle)barStyle { switch (barStyle) { case YHNavigationBarStyleNormal: { self.backgroundColor = [UIColor YHColorWithHex:0xffee00 alpha:1.0f]; _bgImageV.hidden = YES; } break; case YHNavigationBarStyleTransparent: { self.backgroundColor = [UIColor clearColor]; _bgImageV.hidden = NO; } break; case YHNavigationBarStyleWhite: { self.backgroundColor = [UIColor YHColorWithHex:0xffffff alpha:1.0f]; _bgImageV.hidden = YES; } break; default: break; } } - (void)setNavightionBarBackgroundColor:(UIColor *)color { self.backgroundColor = color; _bgImageV.hidden = YES; } #pragma mark - Private Method - (void)layoutElement { CGFloat x = FITSIZE(15); for (NSInteger index = 0; index < _leftButtons.count; index ++) { UIButton* button = _leftButtons[index]; button.frame = CGRectMake(x, KStatusBarHeight+22-button.height/2, button.width, button.height); [self addSubview:button]; x += button.width; } x = self.width; for (NSInteger index = _rightButtons.count - 1; index >= 0; index --) { UIButton* button = _rightButtons[index]; x = x - button.width - FITSIZE(15); button.frame = CGRectMake(x, KStatusBarHeight+22-button.height/2, button.width, button.height); } // 两边都有按钮 if (_leftButtons.count > 0 && _rightButtons.count > 0) { _titleView.frame = CGRectMake(_leftButtons[_leftButtons.count - 1].right, KStatusBarHeight, _rightButtons[0].x - _leftButtons[_leftButtons.count - 1].right, 44.0f); } // 只有左侧有按钮 else if (_leftButtons.count > 0 && (_rightButtons.count == 0 || !_rightButtons )) { _titleView.frame = CGRectMake(_leftButtons[_leftButtons.count - 1].right, KStatusBarHeight,self.width - _leftButtons[_leftButtons.count - 1].width * 2, 44.0f); } // 只有右侧有按钮 else if ((!_leftButtons || _leftButtons.count == 0) && _rightButtons.count > 0) { CGFloat leftWidth = self.width - _rightButtons[0].x; _titleView.frame = CGRectMake(leftWidth, KStatusBarHeight, self.width - leftWidth * 2, 44.0f); } // 两边都没有按钮 else { _titleView.frame = CGRectMake(0, KStatusBarHeight, self.width, 44.0f); } if (!_isCustomTitleView) { CGFloat titleViewDistanceFromLeft = _titleView.x; CGFloat titleViewDistanceFromRight = self.width - _titleView.right; CGFloat titleLabelShouldWidth = 0; if (titleViewDistanceFromLeft > titleViewDistanceFromRight) { titleLabelShouldWidth = (self.width / 2.0f - titleViewDistanceFromLeft) * 2; } else { titleLabelShouldWidth = (self.width / 2.0f - titleViewDistanceFromRight) * 2; } _titleLabel.width = titleLabelShouldWidth; _titleLabel.x = 0; _titleLabel.y = 0; CGFloat titleLabelShouldXOnSelf = (self.width - titleLabelShouldWidth) / 2.0f; CGFloat titleLabelX = [_titleLabel convertRect:_titleLabel.bounds toView:self].origin.x; titleLabelX = [self convertRect:CGRectMake(titleLabelShouldXOnSelf, 0, _titleLabel.width, _titleLabel.height) toView:_titleView].origin.x; _titleLabel.x = titleLabelX; _titleLabel.height = _titleView.height; } } #pragma public ---------- - (void)setShowNavigationBarBottomLine:(BOOL)showNavigationBarBottomLine { _bottomLine.hidden = !showNavigationBarBottomLine; } - (UILabel *)navTitleLabel { return _titleLabel; } @end