情况一:右侧两个按钮
//为导航栏添加右侧按钮1
UIBarButtonItem *right1 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"my_share"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(right1Action:)];
//为导航栏添加右侧按钮2
UIBarButtonItem *right2 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"mycollection"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(right2Action:)];
NSArray *arr = [[NSArray alloc]initWithObjects:right1, right2, nil];
self.navigationItem.rightBarButtonItems = arr;
//右侧导航条点击事件
- (void)right1Action:(UIBarButtonItem *)sender{
}
- (void)right2Action:(UIBarButtonItem *)sender{
}
情况二:导航条中间两个按钮
1:自定义titleView
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 7, SCREEN_WIDTH * 0.5, 30)];
NSArray *titleArr = [NSArray arrayWithObjects:@"单赏",@"稿件(8)", nil];
for (int i = 0; i<2; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = i;
button.frame = CGRectMake( (titleView.dc_width/2) * i, 0, titleView.dc_width/2, titleView.dc_height);
[button addTarget:self action:@selector(doButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:titleArr[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
if (button.tag == 0) {
button.selected = YES;
_selectedBtn = button;
}
//将循环创建的button都添加到view上面
[titleView addSubview:button];
2: 记录选中button
@property (nonatomic, strong) UIButton *selectedBtn;
3:切换按钮点击事件
#pragma mark - button触发的方法
-(void)doButtonAction:(UIButton *)sender {
if (sender != _selectedBtn) {
_selectedBtn.selected = NO;
sender.selected = YES;
_selectedBtn = sender;
}else{
_selectedBtn.selected = YES;
}
}
}
self.navigationItem.titleView = titleView;