0

I want to add two buttons with custom image to Navigation Bar with some specific position.

I found solution But it is for Right/Left Navigation Bar Button.

My code for that is:

 NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
 UIToolbar *tools = [[UIToolbar alloc]
                    initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add Pin button.

UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
[bi1 release];

// Add Hot Spot button.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
[bi2 release];

// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];

 // Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
[twoButtons release];

How can i do this?

user1673099
  • 3,293
  • 7
  • 26
  • 57

4 Answers4

4
UIView *vieww =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[vieww addSubview:yourBtn1];
[vieww addSubview:yourBtn2];

[self.navigationController.navigationBar addSubview:vieww];    

And if you want to remove yourButtonView then make is global object;

in .h

UIView *vieww;

and in .m

-(void)viewWillDisappear:(BOOL)animated
{
    [vieww removeFromSuperview];
}

Or follow this for more Link

Community
  • 1
  • 1
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
  • titleview places the vieww in center. I want to place vieww just right side by the center. – user1673099 Dec 15 '12 at 07:49
  • chk now....or if possible then five a screen shot or adjust your button frame in view.... – Rajneesh071 Dec 15 '12 at 08:04
  • yes,your code is working. But how can i remove subview from the view when on viewDid Unload method?? becuse when i navigate to previous view the two buttons are not remove. – user1673099 Dec 15 '12 at 10:07
  • But I just want to know, Why view is not removed, when i try in view didUnload method to remove it. – user1673099 Dec 15 '12 at 10:29
1

if you are using >iOS 5, then use this.

UIBarButtonItem *btn1=[[UIBarButtonItem alloc] initWithTitle:@" + " style:UIBarButtonItemStyleDone target:self action:@selector(action1:)];
    UIBarButtonItem *btn2=[[UIBarButtonItem alloc] initWithTitle:@" - " style:UIBarButtonItemStyleDone target:self action:@selector(action2:) ];
self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:btn1,btn2,nil];

for < iOS 5 u can use following:

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 160, 44.01)];
        tools.barStyle = UIBarStyleBlackOpaque;
        // create the array to hold the buttons, which then gets added to the toolbar
        NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:4];
        [buttons addObject:btn1];
        [buttons addObject:btn2];
        [tools setItems:buttons animated:NO];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
Nookaraju
  • 1,668
  • 13
  • 24
0

Instead of adding toolbar, you can create one UIView, add two buttons on that view.

    UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:yourView];
Ganee....
  • 302
  • 2
  • 13
0

If you want to do this in combination with using a storyboard, take a look at this question.

Community
  • 1
  • 1
ecotax
  • 1,933
  • 17
  • 22