2012-04-30 2 views
2

私は2つのカスタム右バーボタンアイテムを持っています。ポートレートモードでは、それらは互いにオーバーレイし、それらのうちの1つだけが表示されますが、ランドスケープモードでは両方が表示されます。アイテムは、カスタムビューで作成されます。カスタムビューは、背景イメージを持つUIButtonです。UINavigationitem custom 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

ツールバーは、ツールバーとツールバーを追加するための複数のボタン配列をサポートしています。以下のようにナビゲーション項目に追加します。 – Dinesh

+1

Dineshに感謝しますが、項目をナビゲーション項目に直接設定する方法がある場合はツールバーを使用する理由を理解できません – taffarel

関連する問題