Добавление элемента кнопки панели в панель навигации - PullRequest
7 голосов
/ 25 мая 2011

Я работал с кнопками панели навигации. Я использовал следующий код для этого

UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] 
                                    initWithTitle:@"Save"                                            
                                    style:UIBarButtonItemStyleBordered 
                                    target:self 
                                 action:@selector(save_Clicked:)];
     self.navigationItem.rightBarButtonItem = btnSave;
     [btnSave release];

     UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] 
                                    initWithTitle:@"Cancel"                                            
                                    style:UIBarButtonItemStyleBordered 
                                    target:self 
                                    action:@selector(save_Clicked)];
     self.navigationItem.leftBarButtonItem = btnCancel;
     [btnCancel release];

У меня вопрос, как добавить еще одну кнопку рядом с элементом левой панели. заранее спасибо

Ответы [ 4 ]

8 голосов
/ 25 мая 2011

Для этого вам нужно создать панель инструментов, затем продолжать добавлять к ней UIButton, затем установить панель инструментов как leftBarButton

примерно так:

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 250, 44)];
tools.tintColor = [UIColor clearColor];
[tools setTranslucent:YES];

NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:9];

UIImage *myImage = [UIImage imageNamed:@"AL_HomeMod_Icon.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);

[myButton addTarget:self action:@selector(clickViewHomeMod) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *bi = [[UIBarButtonItem alloc]
                       initWithCustomView:myButton];

[buttons addObject:bi];
[bi release];

myImage = [UIImage imageNamed:@"AL_History_Icon.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);

[myButton addTarget:self action:@selector(clickViewHistory) forControlEvents:UIControlEventTouchUpInside];

bi = [[UIBarButtonItem alloc]
      initWithCustomView:myButton];

[buttons addObject:bi];
[bi release];

myImage = [UIImage imageNamed:@"AL_RX_Icon.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);

[myButton addTarget:self action:@selector(clickViewCustomPopView2) forControlEvents:UIControlEventTouchUpInside];

bi = [[UIBarButtonItem alloc]
      initWithCustomView:myButton];

[buttons addObject:bi];
[bi release];

myImage = [UIImage imageNamed:@"AL_User_Icon.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);

[myButton addTarget:self action:@selector(clickViewCustomPopView:) forControlEvents:UIControlEventTouchUpInside];
bi = [[UIBarButtonItem alloc]
      initWithCustomView:myButton];
[buttons addObject:bi];
popButton = myButton;
[bi release];


// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];

[buttons release];

// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];

надеюсь, что помощь

Пруд

7 голосов
/ 30 сентября 2013

Создать кнопку как

UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] 
                                initWithImage:[UIImage imageNamed:@"logout.png"] 
                                style:UIBarButtonItemStylePlain 
                                target:self action:@selector(doLogout)];

Добавить эту кнопку справа от панели навигации

self.navigationItem.rightBarButtonItem = logoutButton;

или добавить эту кнопку слева от панели навигации

self.navigationItem.leftBarButtonItem = logoutButton;

doLogout - это функция, которая вызывается при сенсорной кнопке выхода из системы

2 голосов
/ 27 августа 2012

Я выполнил свою задачу, используя следующий код:

UIToolbar *tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 150, 44)];

tools.backgroundColor=[UIColor clearColor];

 [tools setTranslucent:YES];

UIBarButtonItem *optionBtn=[[UIBarButtonItem alloc]initWithTitle:@"Options" style:UIBarButtonItemStyleBordered target:self action:@selector(optionPressed:)];

UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(donePressed:)];

 NSArray *buttons=[NSArray arrayWithObjects:optionBtn,doneBtn, nil];

    [tools setItems:buttons animated:NO];

 self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:tools];

ПРИМЕЧАНИЕ. Начиная с IOS 5.0, Apple делает это намного проще. Это можно сделать как

self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:optionBtn,doneBtn, nil];
1 голос
/ 25 мая 2011

Создайте пользовательский вид с помощью двух кнопок и используйте инициализатор UIBarButtonItem initWithCustomView:.Это должно сделать это.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...