0

I have an array of bar buttons that I normally set with something like:

 NSArray *leftButtonArray = [[NSArray alloc]initWithObjects: backBarButton, separator1,postBarButton, separator1, memeBarButton, separator1, pollBarButton, nil];
self.navigationItem.leftBarButtonItems = leftButtonArray;

However, I want to change these buttons on the left to instead be center aligned. Anyone know how I can do that? Providing an example or using my buttons to do so would be a plus.

Thanks!

John
  • 1,677
  • 4
  • 15
  • 26
  • What do you mean by "center aligned"? They are on the left because you made the `leftBarButtonItems`. If you don't want that, don't do that. What _do_ you want? Do you want a bunch of buttons in the center, is that is? – matt Apr 16 '15 at 03:25
  • Buttons in center (I also have a few on left and right separately) – John Apr 16 '15 at 03:26
  • The center of a navigation bar is occupied by the `titleView`. So make a view with some buttons in it, and make it the `titleView`. – matt Apr 16 '15 at 03:28
  • Tried the following from the other post, doesn't work [self.navigationController.toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, postBarButton, separator1, memeBarButton, separator1, pollBarButton, flexibleSpace, nil]]; – John Apr 16 '15 at 03:30
  • The solution you marked earlier might not work because of the following: http://stackoverflow.com/questions/9690409/uinavigationitem-titleview-ignored-if-leftbarbuttonitem-is-set (flexible space is overlapped it seems) – John Apr 16 '15 at 03:33

1 Answers1

2

As I understand your problem it required to have some button to left, some on middle, and some on right with relative separator. Directly navigation will not provide to put button is middle, or in TitleView

You can set custom title view to the navigation bar like this way.

//To hide default back button
self.navigationItem.hidesBackButton=YES;

//Title View for Navigation
UIView *navTitle1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[navTitle1 setBackgroundColor:[UIColor lightGrayColor]];

//Left View button and separator
UIButton *backBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
[backBarButton setTitle:@"Back" forState:UIControlStateNormal];

UIView *ttlview1 = [[UIView alloc] initWithFrame:CGRectMake(51, 0, 1, 44)];
[ttlview1 setBackgroundColor:[UIColor darkGrayColor]];


//Center view with two buttons and seprator for them
UIView *middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 121, 44)];
[middleView setBackgroundColor:[UIColor clearColor]];
[middleView setCenter:navTitle1.center];

UIButton *postBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
[postBarButton setTitle:@"Post" forState:UIControlStateNormal];

UIView *ttlview2 = [[UIView alloc] initWithFrame:CGRectMake(middleView.frame.size.width/2, 0, 1, 44)];
[ttlview2 setBackgroundColor:[UIColor darkGrayColor]];

UIButton *memeBarButton = [[UIButton alloc] initWithFrame:CGRectMake((middleView.frame.size.width/2)+1, 0, 60, 44)];
[memeBarButton setTitle:@"Meme" forState:UIControlStateNormal];

[middleView addSubview:ttlview2];
[middleView addSubview:postBarButton];
[middleView addSubview:memeBarButton];

//Right button with seprator before that
UIView *ttlview3 = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-71, 0, 1, 44)];
[ttlview3 setBackgroundColor:[UIColor darkGrayColor]];

UIButton *pollBarButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-70, 0, 50, 44)];
[pollBarButton setTitle:@"POLL" forState:UIControlStateNormal];

[navTitle1 addSubview:backBarButton];
[navTitle1 addSubview:ttlview1];
[navTitle1 addSubview:middleView];
[navTitle1 addSubview:ttlview3];
[navTitle1 addSubview:pollBarButton];
self.navigationItem.titleView = navTitle1;

It will look like, THIS

May be this would help you.

HTH, Enjoy Coding !!

Viral Savaj
  • 3,379
  • 1
  • 26
  • 39