【问题标题】:Create Blank Space Between Nav Bar Items?在导航栏项目之间创建空白空间?
【发布时间】:2014-04-08 07:45:39
【问题描述】:

我花了几个小时试图解决这个问题。我最后的选择是只创建空白标签来创造空间,但我觉得他们是一种更清洁的方式。

基本上我有三个按钮,我们试图在它们之间创建固定空间以保持整洁。每个按钮都是以编程方式添加的。

我找到了这段代码:

UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItem.width = 20.0f; // or whatever you want

(Source)

但是如何将这段代码分配给某个按钮呢?

【问题讨论】:

    标签: ios uinavigationbar uibarbuttonitem


    【解决方案1】:

    您可能会感到困惑。您不会将此代码分配给按钮。该代码创建了一个UIBarButtonSystemItemFixedSpace 类型的按钮。所以,按照你链接到的答案所说的去做。创建固定和灵活的UIBarButtonItems(以及您拥有的其他按钮),然后将它们设置在您的导航栏上。在这种情况下,它们会出现在导航栏的左上角(通过leftBarButtonItems):

    // Create "Normal" buttons items:
    UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"1" style:UIBarButtonItemStylePlain target:Nil action:nil];
    UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithTitle:@"2" style:UIBarButtonItemStylePlain target:Nil action:nil];    
    UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithTitle:@"3" style:UIBarButtonItemStylePlain target:Nil action:nil];
    
    // Create "Spacer" bar button items 
    UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedItem.width = 20.0f; // or whatever you want
    UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    self.navigationItem.leftBarButtonItems = @[button1, fixedItem, button2, flexibleItem, button3];
    

    另外,如果你有一个工具栏,你可以使用工具栏的items 属性:

    self.toolbar.items = @[button1, fixedItem, button2, flexibleItem, button3];
    

    【讨论】:

    • 间距现在适用于按钮,但是现在按钮已间隔,它们不再适用于指定的选择器操作。这些按钮之前被创建为 UIButton(s),然后作为 UIBarButtonItems 放置在导航栏数组中,这就是它们之前能够运行的方式。你认为我该如何恢复功能?
    • 我们成功了!太感谢了!!我们必须将目标更改为“自我”。
    【解决方案2】:

    如果您想在 Interface Builder 中执行此操作。 我试图拖动一个灵活或固定的空格键项目,但它似乎在导航栏中不起作用。 解决方法:在栏按钮之间拖动一个 UIView。在尺寸检查器中调整 UIView 的宽度,并设置 view 和 Bar Button Item 的颜色为清除颜色。

    【讨论】:

    • 谢谢。我希望在 IB 有更好的方法!找到了吗?
    【解决方案3】:

    您可能会多次将此按钮添加到与其他按钮一起的数组中。然后您将该数组设置为rightBarButtonItems(或leftBarButtonItems)或您的视图控制器导航项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 2016-08-16
      相关资源
      最近更新 更多