How can I add new button in navigationItem? Say I need to add it near right button.
Code used for left right.
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_cancelButton];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_shareButton];
How can I add one more button?
There are Different Approach to add more then one Buttons on right side of navigationcontroller.one of the Simplist way is given below
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
UIButton *exampleButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
exampleButton1.frame = CGRectMake(0, 2, 30, 40);
[exampleButton1 addTarget:self action:@selector(FirstMethod) forControlEvents:UIControlEventTouchUpInside];
[exampleButton1 setImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal];
[buttonView addSubview:exampleButton1];
UIButton *exampleButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
exampleButton2.frame = CGRectMake(70, 2, 30, 40);
[exampleButton2 addTarget:self action:@selector(Second method) forControlEvents:UIControlEventTouchUpInside];
[exampleButton2 setImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal];
[buttonView addSubview:exampleButton2];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
Hope it will be helpful for you.Thanks
you can add more buttons
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:uibarbuttonInstance1, uibarbuttonInstance2, nil];
same for right buttons
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:uibarbuttonInstance1, uibarbuttonInstance2, nil];
Middle Button
UIButton *btnShow = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 34, 34)];
btnShow.backgroundColor = [UIColor colorWithWhite:0.411 alpha:1.000];
[btnShow setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
[btnShow setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateHighlighted];
self.navigationItem.titleView = btnShow;
Right Bar Button
UIButton *tempButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[tempButton1 setFrame:CGRectMake(0, 0, 34, 34)];
tempButton1.tag = 0;
[tempButton1 addTarget:self action:@selector(btnBackClicked:) forControlEvents:UIControlEventTouchUpInside];
[tempButton1 setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
[tempButton1 setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateHighlighted];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:tempButton1]];
Left Bar button
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
[tempButton setFrame:CGRectMake(0, 0, 34, 34)];
[tempButton addTarget:self action:@selector(btnBackClicked:) forControlEvents:UIControlEventTouchUpInside];
[tempButton setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
[tempButton setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateHighlighted];
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:tempButton]
Output
NSArray *tempArray2= [[NSArray alloc] initWithObjects:self.shareBtn,self.gameCenterBtn,nil];
self.navigationItem.leftBarButtonItems=tempArray2;
Try this. It worked for me.
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
flex.width = 20;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 40, 30)];
[btn setImage:[UIImage imageNamed:@"mac-gray.png"] forState:UIControlStateNormal];
UIBarButtonItem *bar1 = [[UIBarButtonItem alloc]initWithCustomView:btn];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setFrame:CGRectMake(0, 0, 40, 30)];
[btn2 setImage:[UIImage imageNamed:@"mac-gray.png"] forState:UIControlStateNormal];
UIBarButtonItem *bar2 = [[UIBarButtonItem alloc]initWithCustomView:btn2];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:bar1,flex,bar2, nil];