类似这样的导航栏 用WKBarButtonUtil那个框架:
- (void)initNav
{
//必要的设置, 如果没有设置可能导致内容显示不正常
// self.edgesForExtendedLayout = UIRectEdgeNone;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.frame = CGRectMake(10, 35/2.0f, 60, 40);
btn.imageEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -30);
btn.titleLabel.textAlignment = NSTextAlignmentLeft;
btn.mj_y = _segment.mj_y-7;
btn.width = 100;
[btn setTitle:@"杭州" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"rightRow_home"] forState:UIControlStateNormal];
CGSize titleSize = self.cBtnLeft.titleLabel.bounds.size;
CGSize imageSize = self.cBtnLeft.imageView.bounds.size;
//文字左移
btn.titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width-10, 0.0, 60);
// UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
//图片右移
btn.imageEdgeInsets = UIEdgeInsetsMake(0.0, titleSize.width, 0.0, -titleSize.width);
btn.titleLabel.font = Font(12.0);
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = [WKBarButtonUtil barButtonWithImage:Image(@"rightRow_home") target:self action:@selector(navRightPressed)];
[btn addTarget:self action:@selector(navLeftPressed) forControlEvents:UIControlEventTouchUpInside];
self.segment = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"优选",@"咨询"]];
_segment.frame =CGRectMake(0, 20, 120, 44);
_segment.verticalDividerWidth = 10;
_segment.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
NSLog(@"segment.frame%@",NSStringFromCGRect(_segment.frame));
NSLog(@"selectionIndicatorHeight%f",_segment.selectionIndicatorHeight);
_segment.selectionIndicatorHeight = 2.0f;
_segment.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
_segment.selectionIndicatorColor = [UIColor redColor];
_segment.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor blackColor]};
_segment.selectedTitleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor redColor]};
_segment.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
[_segment addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = self.segment;
UIView *line = [[UIView alloc] init];
line.frame = (CGRect){0,K_NAV_HEIGHT-1,K_SCREEN_WIDTH,1};
line.backgroundColor = COLORE_BACKGROUNDCOLOR_LIGHTGRAY;
//试验居中
CGRect leftViewbounds = self.navigationItem.leftBarButtonItem.customView.bounds;
CGRect rightViewbounds = self.navigationItem.rightBarButtonItem.customView.bounds;
CGRect frame;
CGFloat maxWidth = leftViewbounds.size.width > rightViewbounds.size.width ? leftViewbounds.size.width : rightViewbounds.size.width;
maxWidth += 15;//leftview 左右都有间隙,左边是5像素,右边是8像素,加2个像素的阀值 5 + 8 + 2
frame = _segment.frame;
frame.size.width = K_SCREEN_WIDTH - maxWidth * 2;
self.navigationItem.titleView.frame = frame;
frame = _segment.frame;
frame.size.width = K_SCREEN_WIDTH - maxWidth * 2;
_segment.frame = frame;
}
- (void)createUI
{
}
- (void)navLeftPressed
{
NSLog(@"clickleft");
}
- (void)navRightPressed
{
NSLog(@"clickRight");
}
- (void)navRightPressed:(UIButton *)sender
{
}
- (void)segmentedControlChangedValue:(HMSegmentedControl *)sender
{
}
缺点:HMSegment下面的线不能如设计稿缩短,暂时先这样待优化.或自己写或找博客
简易修改那条红线可参考