【问题标题】:Creating a UIBarButton Centered Programmatically以编程方式创建居中的 UIBarButton
【发布时间】:2015-06-22 06:32:56
【问题描述】:

我有一组条形按钮,我通常使用以下内容进行设置:

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

但是,我想将左侧的这些按钮更改为居中对齐。有谁知道我该怎么做?提供一个例子或使用我的按钮这样做会是一个加号。

谢谢!

【问题讨论】:

  • “居中对齐”是什么意思?它们位于左侧,因为您创建了 leftBarButtonItems。如果你不想那样,就不要那样做。你想要什么?你想要一堆按钮在中间,是吗?
  • 中间的按钮(我也有一些左右分开的)
  • 导航栏的中心被titleView占据。因此,创建一个包含一些按钮的视图,并将其设为 titleView
  • 从另一篇文章中尝试了以下,不起作用 [self.navigationController.toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, postBarButton, separator1, memeBarButton, separator1, pollBarButton, flexibleSpace, nil]];
  • 您之前标记的解决方案可能无法正常工作,原因如下:stackoverflow.com/questions/9690409/…(弹性空间似乎重叠了)

标签: ios uinavigationcontroller uinavigationbar uibarbuttonitem


【解决方案1】:

据我了解您的问题,它需要有一些按钮在左边,一些在中间,一些在右边,并带有相对分隔符。直接导航不会提供把按钮放在中间,或者在TitleView中

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

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

看起来像,THIS

也许这会对你有所帮助。

HTH,享受编码!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 2011-04-09
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    相关资源
    最近更新 更多