7

i was working with navigation bar button items.i was using the following code to do so

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];

my question is how to add another button just adjacent to the left bar button item. thanks in advance

4 Answers 4

8

To do this you need to create a toolbar then keep adding UIButton to it, then set the toolbar as the leftBarButton

something like this:

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];

hope that help

Pondd

Sign up to request clarification or add additional context in comments.

Comments

7

Create a button as

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

Add this button to right of navigation bar

self.navigationItem.rightBarButtonItem = logoutButton;

or add this button to left side of navigation bar

self.navigationItem.leftBarButtonItem = logoutButton;

doLogout is a function which will be called on touch logout button

Comments

2

I achieved my task by using the following code :

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];

NOTE:From IOS 5.0 onwards, apple has done it lot easier . It can be done as

self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:optionBtn,doneBtn, nil];

Comments

1

Create a custom view with two buttons and use UIBarButtonItem's initWithCustomView: initializer. That should do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.