2012-12-15 122 views
0

我想將具有自定義圖像的兩個按鈕添加到具有某個特定位置的導航欄中。Iphone SDK中的導航欄上添加多個按鈕

我找到了解決方案但它是用於右/左導航欄按鈕。

我該代碼是:

NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2]; 
UIToolbar *tools = [[UIToolbar alloc] 
        initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)]; 
// Add Pin button. 

UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(Edit:)]; 
bi1.style = UIBarButtonItemStyleBordered; 
bi1.width = 45; 
[buttons addObject:bi1]; 
[bi1 release]; 

// Add Hot Spot button. 
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(Add:)]; 
bi2.style = UIBarButtonItemStyleBordered; 
[buttons addObject:bi2]; 
[bi2 release]; 

// Add buttons to toolbar and toolbar to nav bar. 
[tools setItems:buttons animated:NO]; 
[buttons release]; 

// Add toolbar to nav bar. 
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
[tools release]; 
self.navigationItem.rightBarButtonItem = twoButtons; 
[twoButtons release]; 

我怎麼能這樣做呢?

回答

4
UIView *vieww =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
[vieww addSubview:yourBtn1]; 
[vieww addSubview:yourBtn2]; 

[self.navigationController.navigationBar addSubview:vieww];  

如果你想刪除yourButtonView然後作出是全球性的對象;

in .h 

UIView *vieww; 

.m

-(void)viewWillDisappear:(BOOL)animated 
{ 
    [vieww removeFromSuperview]; 
} 

或者,如果您使用的是按照這個更多Link

+0

titleview將視圖置於中心。我想把vieww放在正中央的右邊。 – user1673099

+0

chk現在........ – Rajneesh071

+0

chk現在....或者如果可能的話,然後五個屏幕截圖或調整您的按鈕框架的視圖.... – Rajneesh071

0

除了添加工具欄,您可以創建一個UIView,在該視圖上添加兩個按鈕。

UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:yourView]; 
1

>的iOS 5,然後用這個。

UIBarButtonItem *btn1=[[UIBarButtonItem alloc] initWithTitle:@" + " style:UIBarButtonItemStyleDone target:self action:@selector(action1:)]; 
    UIBarButtonItem *btn2=[[UIBarButtonItem alloc] initWithTitle:@" - " style:UIBarButtonItemStyleDone target:self action:@selector(action2:) ]; 
self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:btn1,btn2,nil]; 

爲<的iOS 5 u可以使用下列內容:如果你想用一個故事板做這個組合

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 160, 44.01)]; 
     tools.barStyle = UIBarStyleBlackOpaque; 
     // create the array to hold the buttons, which then gets added to the toolbar 
     NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:4]; 
     [buttons addObject:btn1]; 
     [buttons addObject:btn2]; 
     [tools setItems:buttons animated:NO]; 
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];