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; 
+0

檢查這... HTTP://stackoverflow.com/questions/31612471/how-can -i-set-two-buttons-on-the-right-of-uinavigationbar/31612665#31612665 –

回答

0

創建一個數組欄按鈕。和setLeftBarButtonItems不setLeftBarButtonItem爲:

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

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

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

希望它會有助於您

+0

Thanks.But此代碼也顯示一個錯誤。 – raghu

+0

我會upvote,如果你給答案..... – raghu