2012-04-30 48 views
2

我有2個自定義右鍵欄按鈕項目,並且在縱向模式下它們彼此重疊,只有其中一個可見,但在橫向模式下都可見。項目是用自定義視圖創建的,它是帶有背景圖像的UIButton。UINavigationitem自定義rightBarButtonItems

optionsBUtton=[UIButton buttonWithType:UIButtonTypeCustom]; 
[optionsBUtton setImage:[UIImage imageNamed:@"optionsIcon.png"] forState:UIControlStateNormal]; 
[optionsBUtton setBackgroundImage:[UIImage imageNamed:@"optionsBtn.png"] forState:UIControlStateNormal]; 
[optionsBUtton sizeToFit]; 
UIBarButtonItem* btnOptions=[[UIBarButtonItem alloc] initWithCustomView:optionsBUtton]; 

searchButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
    [searchButton setImage:[UIImage imageNamed:@"searchIcon.png"] forState:UIControlStateNormal]; 
    [searchButton setBackgroundImage:[UIImage imageNamed:@"optionsBtn.png"] forState:UIControlStateNormal]; 
    [searchButton sizeToFit]; 
    UIBarButtonItem* btnSearch=[[UIBarButtonItem alloc] initWithCustomView:searchButton]; 

rightButtonItems=[[NSArray alloc] initWithObjects:btnOptions,btnSearch, nil]; 
    navItem.rightBarButtonItems=rightButtonItems; 

回答

2

您必須用戶工具欄和設置的按鈕工具欄下面是示例代碼

// create a toolbar where we can place some buttons 
UIToolbar* toolbar = [[UIToolbar alloc] 
         initWithFrame:CGRectMake(0, 0, 100, 45)]; 
[toolbar setBarStyle: UIBarStyleBlackOpaque]; 

// create an array for the buttons 
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3]; 

// create a standard save button 
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
    target:self 
    action:@selector(saveAction:)]; 
saveButton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:saveButton]; 
[saveButton release]; 

// create a spacer between the buttons 
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
    target:nil 
    action:nil]; 
[buttons addObject:spacer]; 
[spacer release]; 

// create a standard delete button with the trash icon 
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemTrash 
    target:self 
    action:@selector(deleteAction:)]; 
deleteButton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:deleteButton]; 
[deleteButton release]; 

// put the buttons in the toolbar and release them 
[toolbar setItems:buttons animated:NO]; 
[buttons release]; 

// place the toolbar into the navigation bar 
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
              initWithCustomView:toolbar] autorelease]; 
[toolbar release]; 

謝謝..!

+1

爲什麼使用工具欄如果有按鈕項收集屬性? – taffarel

+0

工具欄支持多個按鈕數組添加工具欄和工具欄添加到導航項,就像下面的代碼self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease]; – Dinesh

+1

謝謝Dinesh,但我不明白爲什麼使用工具欄,如果有一種方法可以直接將items設置爲navigationitem – taffarel