Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow a UIBarButtonItem to touch top & bottom of UINavigationBar

Take a look at the Droplr iPhone app:

enter image description here

Notice how the UIBarButtonItems are able to touch the right, left, top, and bottom of the screen/navigation bar?

How can I achieve something similar? Here's how I make a sample UIBarButton and set it to the right item:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];    
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *bb = [[[UIBarButtonItem alloc] initWithCustomView:button]autorelease];
[self.navigationItem setRightBarButtonItem:bb animated:YES];

However, it is not right aligned, and has quite a bit of margin from the top & the bottom. My image size is correct (44px), and it looks like it shrinks it to fit a frame of sorts.

So, how can I do this?


Edit: Whoops, the top/bottom spacing was my fault. However, I can't figure out how to align the bar button flush with the left/right side. Here's what I mean: (sorry for the ugly button, it was just a test)

enter image description here

I tried setting the image inserts, but it didn't seem to do anything.

like image 439
sudo rm -rf Avatar asked Jun 15 '11 04:06

sudo rm -rf


1 Answers

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



UIButton *searchbutton = [[UIButton alloc] initWithFrame:CGRectMake(2,2,50, 30)];
[searchbutton setImage:[UIImage imageNamed:@"some-pic.png"] forState: UIControlStateNormal];
[rightview addSubview:searchbutton];


UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:rightview];
self.navigationItem.rightBarButtonItem = customItem;
[customItem release];

I use a customView for the rightBarButtonItem and I get it right aligned. Just try a bit with the CGRectMake-Numbers for the x-coordinate, for testing I added to high numbers...

like image 177
Quirin Avatar answered Oct 29 '22 11:10

Quirin