【问题标题】:Adding buttons to toolbar programmatically in swift以编程方式快速将按钮添加到工具栏
【发布时间】:2016-01-30 19:21:57
【问题描述】:

我很难在 swift 中向工具栏添加按钮,您可以在下面看到我所追求的工具栏的图像,不幸的是,即使我在我的 Storyboard 文件中设计了它,但它并没有将工具栏设置为可见时显示。

我设计这个的方式是两个项目,第一个是flexable space 元素,第二个是add 元素。它看起来像这样:

这是我用来尝试在代码中复制它的代码:

self.navigationController?.toolbarHidden = false
self.navigationController?.toolbarItems = [UIBarButtonItem]()
self.navigationController?.toolbarItems?.append(
    UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
)
self.navigationController?.toolbarItems?.append(
    UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onClickedToolbeltButton:")
)

如您所见,我将工具栏设置为可见,初始化(并清除)UIBarButtonItem 的 toolbarItems 数组,然后以正确的顺序将两个 UIBarButtonItem 添加到数组中。

但是,工具带仍然是空的,这是为什么呢?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    以上都不适合我,但是:

    斯威夫特 3 / 斯威夫特 4

    self.navigationController?.isToolbarHidden = false
    
    var items = [UIBarButtonItem]()
    
    items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) )
    items.append( UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(add)) ) // replace add with your function
    
    self.toolbarItems = items // this made the difference. setting the items to the controller, not the navigationcontroller
    

    【讨论】:

    • 这行得通,但真正的问题是其他人在做什么?
    • 向下滚动的人将获得奖励。谢谢。
    • 这应该是公认的答案,但是是的,它与公认的答案有何不同?
    【解决方案2】:

    通常的方法是创建工具栏项数组,然后将该数组分配给工具栏的items 属性。

    self.navigationController?.isToolbarHidden = false
    var items = [UIBarButtonItem]()
    items.append(
        UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    )
    items.append(
        UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(onClickedToolbeltButton(_:)))
    )
    toolbarItems = items
    

    【讨论】:

    • 如果action为nil,为什么要设置target为self?
    • 正如@DavidSeek 所述,UIViewController 上的self.toolbarItems 是设置工具栏按钮的正确方法。
    • 哦,只知道navigationController 有一个内置的toolbar,我创建了自己的toolbar 并在之前以编程方式设置了它的约束,真是错过了:)
    【解决方案3】:
    self.navigationController?.toolbarItems = items
    
    self.navigationController?.setToolbarItems(items, animated: false)
    
    self.navigationController?.toolbar.setItems(items, animated: false)
    

    试试看。

            self.navigationController?.toolbarHidden = false
            var items = [UIBarButtonItem]()
            items.append(
                UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
            )
            items.append(
                UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onClickedToolbeltButton:")
            )
    
            self.navigationController?.toolbar.setItems(items, animated: false)
    

    【讨论】:

      【解决方案4】:

      这是MKUserTrackingBarButtonItem 的示例:

      navigationController?.toolbarHidden = false
      let barButtonItem = MKUserTrackingBarButtonItem(mapView: self.mapView)
      self.toolbarItems = [barButtonItem]
      

      【讨论】:

        【解决方案5】:

        使用当前选择器语法更新答案

        斯威夫特 3

        var barButtons = [UIBarButtonItem]()
        barButtons.append(
            UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(ThisViewController.onDoneBarButtonClick))
        )
        self.navigationItem.setRightBarButtonItems(barButtons, animated: false)
        

        您可以将此代码放在任何加载事件中。它在 viewDidLoad() 中对我来说是无缝的。

        将“ThisViewController.onDoneBarButtonClick”替换为您的视图控制器类名称以及您要编写的用于管理工具栏按钮单击的任何方法。

        【讨论】:

          【解决方案6】:
          let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addSomething:")
          toolbarItems = [UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil),addButton]
          self.navigationController!.setToolbarHidden(false, animated: false)
          

          【讨论】:

            猜你喜欢
            • 2017-10-28
            • 2015-04-15
            • 1970-01-01
            • 1970-01-01
            • 2020-09-23
            • 2014-01-03
            • 2020-05-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多