如何在导航栏中添加多个栏按钮
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在导航栏中添加多个栏按钮相关的知识,希望对你有一定的参考价值。
最初我创建了一个名为'mainView'的动态视图。在那里,我添加了导航栏。之后,我想在导航栏中添加2个右边的按钮。如果我这样编码,它会显示带导航栏的视图。但它没有显示正确的条形按钮。
1.是否可以在没有导航控制器的情况下添加导航栏?
2.为什么右边的条形按钮没有显示?
//mainView Creation
UIView *mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
mainView.backgroundColor =[UIColor grayColor];
[self.view addSubview:mainView];
//NavigationBar
UINavigationBar *NavigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
NavigationBar.backgroundColor = [UIColor orangeColor];
[mainView addSubview:NavigationBar];
//rightbarView Creation
UIView *rightbarView = [[UIView alloc] init];
rightbarView.backgroundColor = [UIColor orangeColor];
//powerCut Button
UIButton *powerCutbutton = [[UIButton alloc] initWithFrame:CGRectMake(150, 20, 50, 14.01)];
[rightbarView addSubview:powerCutbutton];
UIImage *powercutimage = [UIImage imageNamed:@"powercut.jpg"];
[powerCutbutton setImage:powercutimage forState:UIControlStateNormal];
// addConnection button
UIButton *addConnectionbutton = [[UIButton alloc] initWithFrame:CGRectMake(210, 20, 50, 14.01)];
[rightbarView addSubview:addConnectionbutton];
UIImage *addimage = [UIImage imageNamed:@"add.png"];
[addConnectionbutton setImage:addimage forState:UIControlStateNormal];
// Barbutton item
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightbarView];
//[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:powerCutbutton, addConnectionbutton, nil]];
// setting rightbar button item
self.navigationItem.rightBarButtonItem = item;
答案
创建一个条形按钮数组。和setLeftBarButtonItems不是setLeftBarButtonItem:
UIBarButtonItem * barbtn1;
UIBarButtonItem * barbtn2;
UIBarButtonItem * barbtn3;
[self.navigationItem setLeftBarButtonItems:@[barbtn1,barbtn2,barbtn3]];
要么
[self.navigationItem setRightBarButtonItems:@[barbtn1,barbtn2,barbtn3]];
希望它对你有所帮助
以上是关于如何在导航栏中添加多个栏按钮的主要内容,如果未能解决你的问题,请参考以下文章
如何在 uiviewcontroller 的导航栏中添加后退按钮?