Add multiple Buttons in Navigation Bar

2019-03-29 22:04发布

Can any one help me to add more than one custom button to the right bar of the navigation bar. If possible please answer with the detail code, so that i can understand it properly.

3条回答
男人必须洒脱
2楼-- · 2019-03-29 22:37

//add a right btn to the navigation bar

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 75.0f, 30.0f)];

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn1 setFrame:CGRectMake(0.0f, 0.0f, 30.0f, 30.0f)];
[btn1 setTitle:@"1" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(btn1Tap:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btn1];

UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn2 setFrame:CGRectMake(35.0f, 0.0f, 30.0f, 30.0f)];
[btn2 setTitle:@"2" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(btn2Tap:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btn2];

UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:customView];
[self.navigationItem setRightBarButtonItem:rightBtn];
查看更多
闹够了就滚
3楼-- · 2019-03-29 22:37

Pls go through this link. It will solve your problem. Happy coding

查看更多
小情绪 Triste *
4楼-- · 2019-03-29 22:40

Since iOS 5 there are this 4 methods available

- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated;
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated;
- (void)setLeftBarButtonItems:(NSArray *)items;
- (void)setRightBarButtonItems:(NSArray *)items;

where you can set an array of UIBarButtonItem

example:

NSArray * buttons = @[button1,button2];
[self.navigationItem setRightBarButtonItems:buttons];
查看更多
登录 后发表回答