Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spacing rightBarButtonItems in UINavigationBar

i am using the rightBarButtonItems property of UINavigationBar to add two buttons on the right part of my navigation bar. is it possible to make the spacing between these two buttons wider?

thanks

like image 577
atnatn Avatar asked Feb 09 '12 02:02

atnatn


2 Answers

You can add an UIBarButtonSystemItemFlexibleSpace item between your two buttons.

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                      target:nil
                                                                      action:nil];
like image 77
fannheyward Avatar answered Oct 20 '22 01:10

fannheyward


I was not able to get the flexible space to work in my situation but here is the code I used to be able to position the rightBarButtonItem: Note, I put a border around the UIView so you can see what it looks like with having the image in there.

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(89,40,100,30)];
containerView.layer.borderColor = [[UIColor redColor] CGColor];
containerView.layer.borderWidth = 1.0;
UIImage *image = [UIImage imageNamed:@"nav-icon.png"];
UIButton *navigationButton = [UIButton buttonWithType:UIButtonTypeCustom];
[navigationButton setFrame:CGRectMake(67,0,25,25)];
[navigationButton setImage:image forState:UIControlStateNormal];
[containerView addSubview:navigationButton];

UIBarButtonItem *navigationBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:containerView];

self.navigationItem.rightBarButtonItem = navigationBarButtonItem;
like image 34
Flea Avatar answered Oct 19 '22 23:10

Flea