atnatn
atnatn

Reputation: 238

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

Upvotes: 8

Views: 5827

Answers (2)

Flea
Flea

Reputation: 11284

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;

Upvotes: 1

fannheyward
fannheyward

Reputation: 19297

You can add an UIBarButtonSystemItemFlexibleSpace item between your two buttons.

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                      target:nil
                                                                      action:nil];

Upvotes: 6

Related Questions