2012-04-30 6 views
2

У меня есть две пользовательские кнопки правой панели, а в портретном режиме они накладываются друг на друга, и только один из них виден, но в ландшафтном режиме оба видны. элементы создаются с настраиваемым представлением, которое является 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

панель инструментов поддерживает множество кнопок, чтобы добавить панель инструментов и панель инструментов, добавить в навигационную функцию, как показано ниже код self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView: панель инструментов] autorelease]; – Dinesh

+1

спасибо Dinesh, но я не могу понять, зачем использовать панель инструментов, если есть способ установить элементы непосредственно в navigationitem – taffarel

Смежные вопросы