如何在导航栏中添加多个栏按钮

时间:2015-12-23 13:04:16

标签: ios navigation uinavigationbar uibarbuttonitem rightbarbuttonitem

最初我创建了一个名为'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;

1 个答案:

答案 0 :(得分:1)

创建一个条形按钮数组。和setLeftBarButtonItems不是setLeftBarButtonItem:

UIBarButtonItem * barbtn1;
UIBarButtonItem * barbtn2;
UIBarButtonItem * barbtn3;

[self.navigationItem setLeftBarButtonItems:@[barbtn1,barbtn2,barbtn3]];

[self.navigationItem setRightBarButtonItems:@[barbtn1,barbtn2,barbtn3]];

希望它对你有所帮助