如何在导航栏上添加2个按钮?

时间:2013-03-13 07:17:38

标签: iphone ios objective-c uisplitviewcontroller uibarbuttonitem

我正在使用拆分视图控制器,它在第二个视图控制器上有2个视图控制器我想在导航控制器的右侧添加两个按钮。 我使用以下代码添加一个有效的按钮:

UIBarButtonItem *barButton=[[UIBarButtonItem alloc] init];
    [barButton setCustomView:btnShare];
    self.navigationItem.rightBarButtonItem=barButton;

尝试了此链接http://osmorphis.blogspot.in/2009/05/multiple-buttons-on-navigation-bar.html 但是不能成功。请帮我解决这个问题。

9 个答案:

答案 0 :(得分:6)

试试这个

NSMutableArray *arrRightBarItems = [[NSMutableArray alloc] init];
UIButton *btnSetting = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSetting setImage:[UIImage imageNamed:@"settings.png"] forState:UIControlStateNormal];
btnSetting.frame = CGRectMake(0, 0, 32, 32);
btnSetting.showsTouchWhenHighlighted=YES;
[btnSetting addTarget:self action:@selector(onSettings:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnSetting];
[arrRightBarItems addObject:barButtonItem];

UIButton *btnLib = [UIButton buttonWithType:UIButtonTypeCustom];
[btnLib setImage:[UIImage imageNamed:@"library.png"] forState:UIControlStateNormal];
btnLib.frame = CGRectMake(0, 0, 32, 32);
btnLib.showsTouchWhenHighlighted=YES;
[btnLib addTarget:self action:@selector(onMyLibrary:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:btnLib];
[arrRightBarItems addObject:barButtonItem2];

UIButton *btnRefresh = [UIButton buttonWithType:UIButtonTypeCustom];
[btnRefresh setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal];
btnRefresh.frame = CGRectMake(0, 0, 32, 32);
btnRefresh.showsTouchWhenHighlighted=YES;
[btnRefresh addTarget:self action:@selector(onRefreshBtn:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:btnRefresh];

[arrRightBarItems addObject:barButtonItem1];
self.navigationItem.rightBarButtonItems=arrRightBarItems;

答案 1 :(得分:3)

 [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:[GlobalMethods buttonWithImage:@"btn_home" heighlightImageName:@"btn_home_h" buttonFrame:CGRectMake(2, 1, 34, 34) selectorName:@selector(buttonHomeClicked:) target:self]]];

  UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [tempButton setFrame:CGRectMake(240, 5, 34, 34)];
  [tempButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
  [tempButton setImage:[self getImageFromResource:normalImageName] forState:UIControlStateNormal];
  [tempButton setImage:[self getImageFromResource:heighlightImageName] forState:UIControlStateHighlighted];
  [tempButton setImage:[self getImageFromResource:@"btn_fav_h"] forState:UIControlStateSelected];
  [self.navigationController.navigationBar addSubview:self.buttonFavorite];

答案 2 :(得分:3)

创建两个uibarbuttonitem并在数组中添加两个,然后在导航栏中添加整个数组

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAttachmentClicked:)];
UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] initWithTitle:LS(@"Send") style:UIBarButtonItemStyleBordered target:self action:@selector(sendClicked:)];
self.navigationItem.rightBarButtonItems = @[addButton,sendButton];

试试代码

答案 3 :(得分:2)

UIButton *btnLogOut = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLogOut.frame = CGRectMake(0, 0, 62, 31);
    [btnLogOut setImage:[UIImage imageNamed:@"logout_new.png"] forState:UIControlStateNormal];
    [btnLogOut addTarget:self action:@selector(logoutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *logOutBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnLogOut];

    UIButton *btnError = [UIButton buttonWithType:UIButtonTypeCustom];
    btnError.frame = CGRectMake(0, 0, 62, 31);
    [btnError setImage:[UIImage imageNamed:@"list-icon.png"] forState:UIControlStateNormal];
    [btnError addTarget:self action:@selector(logoutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];


    UIBarButtonItem *errorButton = [[UIBarButtonItem alloc] initWithCustomView:btnError];


    NSArray *navigationBarBtnArray=[NSArray arrayWithObjects:errorButton,logOutBarButton, nil];



     self.navigationItem.rightBarButtonItems=navigationBarBtnArray;

答案 4 :(得分:1)

试试这个

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
    style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];

答案 5 :(得分:1)

只需使用UISegmentedControl代替UIButton

然后将模式更改为"瞬间"并在变更时添加行动。

触发操作后,请检查selectedSegmentIndex以了解选择了哪个细分。

答案 6 :(得分:1)

UIView添加您想要的任意数量的按钮。然后使用

UIBarButtonItem *myBarbtn=[[UIBarButtonItem alloc] initWithCustomView:buttonsView];
self.navigationItem.rightBarButtonItem = myBarbtn;

答案 7 :(得分:1)

对此

使用分段控制
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray array]];
[segmentedControl setMomentary:YES];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"sample1.png"] atIndex:0 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"sample2.png"] atIndex:1 animated:NO];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];

UIBarButtonItem * segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl];
self.navigationItem.rightBarButtonItem = segmentBarItem;

答案 8 :(得分:1)

制作一个UIBarButtonItem的数组并将其传递给

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

UINavigationItem的方法。