如何添加导航栏上的2个按钮?(How to add 2 buttons on navigation

2019-08-17 13:14发布

我使用拆分视图控制器 ,其具有所述第二视图控制器我是假设加上导航控制器的右侧两个按钮上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但不能succeed.Please帮我解决这个问题。

Answer 1:

试试这个

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;


Answer 2:

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


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

尝试THI代码



Answer 4:

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;


Answer 5:

试试这个

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


Answer 6:

做出UIView添加你想要它作为许多按钮。 然后用

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


Answer 7:

采用分段控制这

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;


Answer 8:

组成的数组UIBarButtonItem S和它传递给

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

的方法UINavigationItem



Answer 9:

只需使用一个UISegmentedControl代替UIButton

然后将模式更改为“瞬间”,并在更改添加一个动作。

当动作被触发,检查selectedSegmentIndex知道选择什么段。



文章来源: How to add 2 buttons on navigation bar?