以编程方式创build一个UIBarButton

我有一个通常用类似下面的方式设置的酒吧button的数组:

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

不过,我想改变这些左边的button,而不是中心alignment。 任何人都知道我可以做到这一点? 提供一个例子或使用我的button来这样做将是一个加号。

谢谢!

据我了解你的问题,它需要有一些button,左侧,一些中间,一些右侧与相对分隔符。 直接导航将不会提供放中button,或在标题视图

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

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

它会看起来像这样

可能是这会帮助你。

HTH,享受编码!