Ios 以编程方式创建以UIBarButton为中心的

Ios 以编程方式创建以UIBarButton为中心的,ios,uinavigationcontroller,uinavigationbar,uibarbuttonitem,Ios,Uinavigationcontroller,Uinavigationbar,Uibarbuttonitem,我有一系列的条形按钮,通常设置如下: NSArray *leftButtonArray = [[NSArray alloc]initWithObjects: backBarButton, separator1,postBarButton, separator1, memeBarButton, separator1, pollBarButton, nil]; self.navigationItem.leftBarButtonItems = leftButtonArray; 但是,我想将左侧的这

我有一系列的条形按钮,通常设置如下:

 NSArray *leftButtonArray = [[NSArray alloc]initWithObjects: backBarButton, separator1,postBarButton, separator1, memeBarButton, separator1, pollBarButton, nil];
self.navigationItem.leftBarButtonItems = leftButtonArray;
但是,我想将左侧的这些按钮改为居中对齐。有人知道我怎么做吗?提供一个例子或使用我的按钮这样做将是一个加分


谢谢

据我所知,您的问题需要在左侧有一些按钮,中间有一些按钮,右侧有一些按钮带有相对分隔符。直接导航将不提供将按钮放在中间,或在标题视图中

您可以这样将自定义标题视图设置为导航栏

//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;
看起来

也许这对你有帮助


享受编码的乐趣

你说的“中心对齐”是什么意思?它们位于左侧,因为您制作了
leftBarButtonims
。如果你不想那样,就不要那样做。你想要什么?你想在中间有一堆按钮,是吗?中间的按钮(我在左边和右边各有几个)导航栏的中心被标题视图占据。因此,创建一个包含一些按钮的视图,并将其设置为
标题视图
。从另一篇文章尝试了以下操作,但不起作用[self.navigationController.toolbar setItems:[NSArray arrayWithObjects:flexibleSpace,postBarButton,separator1,memeBarButton,separator1,pollBarButton,flexibleSpace,nil];您前面标记的解决方案可能不起作用,原因如下:(弹性空间似乎重叠)