Creating software-oriented programming UIBarButton

I have a set of buttons on a panel which I usually set with:

 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 center align instead. Does anyone know how I can do this? Providing an example or using my buttons to do this would be a plus.

Thank!

0


source to share


1 answer


As I understand your problem, it should have some button on the left and some in the middle and some on the right with a relative delimiter. Direct navigation will not have the set button in the middle or in the TitleView

You can customize the header view for the navbar as follows.

//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

Maybe this will help you.

HTH, enjoy coding!

+2


source







All Articles