Creating a UIBarButton Centered Programmatically

后端 未结 1 1593
醉酒成梦
醉酒成梦 2020-12-22 11:39

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

 NSArray *leftButtonArray = [[NSArray alloc]initWithObjects: backBarButton, separato         


        
相关标签:
1条回答
  • 2020-12-22 12:39

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

    0 讨论(0)
提交回复
热议问题