【问题标题】:How to show button in center of toolbar?如何在工具栏中心显示按钮?
【发布时间】:2013-07-31 11:36:31
【问题描述】:

我想在工具栏的顶部和中心显示按钮。我设置了 UIBarButtonItem 的图像,但它没有给出如下图所示的预期结果。

我可以添加菜单并找到我

无法添加请求按钮,如图所示。

Request BUtton 是一个图像,当我将它设置为 UIBarButtonItem 图像时,它会被拉伸

请帮忙

这就是我所做的。

但我想这样做。如您所见,在我的解决方案中,请求按钮位于工具栏下方,而在下图中,请求按钮位于顶部。

【问题讨论】:

  • 在这里你想在中心显示这两个按钮,为此只需添加 UIBarButtonItem 类型为 UIBarButtonSystemItemFlexibleSpace 所以它用按钮调整两侧并将这些按钮设置为中心查看我的答案..: )

标签: iphone ios ipad toolbar


【解决方案1】:

只需将UIBarButtonItemFlexibleSpace 添加到这两个按钮的两侧,如下所示..

UIBarButtonItem *flexibleSpace =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

用两个按钮查看所有代码..

UIBarButtonItem *flexibleSpace =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIBarButtonItem *locateMe = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(locateMe:)];

UIBarButtonItem *request = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(request:)];

UIBarButtonItem *menu = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(menu:)];

NSArray *toolbarItems = [NSArray arrayWithObjects:locateMe,flexibleSpace,request , flexibleSpace, menu];

[toolBar setItems:toolbarItems animated:NO];

XIB 你也可以设置这个UIBarButtonSystemItemFlexibleSpace,如下图所示..

更新:您想在 UIToolBar 上添加带有该图像的请求按钮,然后只需在添加 UIToolBar 后将 UIImageView 和按钮带到 UIView 上,如下所示..

[self.view bringSubviewToFront:imgView];
[self.view bringSubviewToFront:btnRequest];

【讨论】:

  • @NG_SE 我知道您在工具栏上只有两个按钮,对吧?所以我写了你想要的确切代码:)
  • @Paras.. 我需要中间按钮的解决方案
  • @NG_SE 是的,现在看到更新的答案,您是否在工具栏中添加了该请求按钮??
  • @ParasJoshi 你弄错了一个 Middel 按钮更大的 joiche 就像我发布了演示链接和图片
  • 是的,我知道伙计,所以等待 NG_SE 回复他是否将其添加到工具栏中
【解决方案2】:

从 Nib 中使用 UIToolbar 对象上的灵活空间项。如下图:-

对于添加中心按钮 Bigger 然后 ToolBar Hight 然后检查下面的演示并使用它的类别来执行此任务只需将您的图像拖到演示中并将图像名称更改为 CenterButtonIconPaw.png 并检查:-

https://github.com/el-eleven/EEToolbarCenterButton

使用我完成的图像,它看起来与您的需求相似:-

【讨论】:

  • 谢谢,这正是我需要的。
【解决方案3】:

斯威夫特版本

    let flexibleSpace = UIBarButtonItem(
                        barButtonSystemItem: .flexibleSpace,
                        target: nil,
                        action: nil)

    let locateMe = UIBarButtonItem(
                        barButtonSystemItem: .plain,
                        target: self,
                        action: #selector(locateMe))

    let request = UIBarButtonItem(
                        barButtonSystemItem: .plain,
                        target: self,
                        action: #selector(request))

    let menu = UIBarButtonItem(
                        barButtonSystemItem: .plain,
                        target: self,
                        action: #selector(menu))

    let items = [locateMe, flexibleSpace, request, flexibleSpace, menu]

    setToolbarItems(items, animated: false)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多