Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding bar button item in navigation bar

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

like image 349
sujay Avatar asked May 25 '11 05:05

sujay


People also ask

How do I customize my navigation bar?

From Settings, tap Display, and then tap Navigation bar. Make sure Buttons is selected, and then you can choose your desired button setup at the bottom of the screen. Note: This option will also affect the location you swipe when using Swipe gestures.

How do I add a bar button in Swift?

You need to open the storyboard, delete the view controller that you have, press cmd , shift , l , and then search for navigation controller . Drag that onto the storyboard. You now need to click on the navigation controller and set it to be the is initial view controller under the attributes inspector .


1 Answers

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

like image 187
Suwitcha Sugthana Avatar answered Sep 28 '22 03:09

Suwitcha Sugthana