views:

534

answers:

2
[Button1 setFrame:CGRectMake(0, 0, 50, 0)];
[Button2 setFrame:CGRectMake(0, 0, 120, 0)];
[Button3 setFrame:CGRectMake(0, 0, 50, 0)];


self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Button1];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Button2];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Button3];

I want to add button in the NavigationBar. NavigationBar have 3 buttons.

Navigation Composition like this.

button 1  button 2          text          button3

But I didn't show that. this like.

          button 2          text          button3

Button1, Button2 and Button3 are an Image.

I thought that Button1 setFrame didn't work.

I think setFrame:CGRectMake(0,0,50,0) works Button1 and setFrame:CGRectMake(0,0,120,0) works Button2. so, Button1 is erased by Button2 setFrame.

How to work this Button1 setFrame ?

Please help me.

+1  A: 

It has nothing to do with the setFrame: calls. You cannot assign two buttons to leftBarButtonItem. The second assignment overwrites the first. You should create a blank view, add the two buttons to this view (setting their frames correctly so that they are located side by side), and then create a bar button item with this container view.

Ole Begemann
Also note that you're leaking all three UIBarButtonItems. You need to release or autorelease these after assigning them to the buttonItem properties.
Rob Napier
Thanks! Thanks!
Beomseok
Good point by Rob. I forgot to mention that.
Ole Begemann
A: 

if you needed, reference this source

UIView *leftButtonItemView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

UIImageView *addButtonEmptyImage = [[UIImageView alloc]init];
[addButtonEmptyImage setImage:[UIImage imageNamed:@"black.png"]];
addButtonEmptyImage.frame = CGRectMake(15,0,20,30);

[leftButtonItemView addSubview:addButtonEmptyImage];
[addButtonEmptyImage release];

UIImageView *correctButtonEmptyImage = [[UIImageView alloc]init];
[correctButtonEmptyImage setImage:[UIImage imageNamed:@"black.png"]];
correctButtonEmptyImage.frame = CGRectMake(50,0,20,30);

[leftButtonItemView addSubview:correctButtonEmptyImage];
[correctButtonEmptyImage release];

UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButtonItemView];
[leftButtonItemView release];

self.navigationItem.leftBarButtonItem = leftButtonItem;
[leftButtonItem release];
Beomseok