0

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

1 回答 1

1

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

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

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

或者

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

希望对你有帮助

于 2015-12-23T13:20:33.447 回答