在导航栏中添加多个按钮

时间:2011-04-20 05:00:20

标签: iphone ios uinavigationcontroller

任何人都可以帮我添加多个自定义按钮到导航栏的右侧栏。 如果可能,请回答详细代码,以便我能够正确理解。

3 个答案:

答案 0 :(得分:10)

//在导航栏中添加一个正确的btn

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];

答案 1 :(得分:5)

请完成此link。它会解决你的问题。快乐的编码

答案 2 :(得分:4)

从iOS 5开始,有4种方法可用

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

您可以在其中设置UIBarButtonItem

数组

示例:

NSArray * buttons = @[button1,button2];
[self.navigationItem setRightBarButtonItems:buttons];