Adding buttons to toolbar programmatically in swift

前端 未结 6 1569
天命终不由人
天命终不由人 2021-02-04 03:14

I\'m having a hard time adding a button to the toolbar in swift, below you can see an image of the toolbar that I\'m after, unfortunately even though I have it designed in my St

相关标签:
6条回答
  • 2021-02-04 03:19
    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)
    
    0 讨论(0)
  • 2021-02-04 03:23
    self.navigationController?.toolbarItems = items
    
    self.navigationController?.setToolbarItems(items, animated: false)
    
    self.navigationController?.toolbar.setItems(items, animated: false)
    

    Try it.

            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)
    
    0 讨论(0)
  • 2021-02-04 03:26

    The usual way to do that is to create the array of toolbar items and then assign the array to the items property of the toolbar.

    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
    
    0 讨论(0)
  • 2021-02-04 03:31

    None of the above worked for me, but:

    Swift 3 / Swift 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
    
    0 讨论(0)
  • 2021-02-04 03:37

    Here is an example with MKUserTrackingBarButtonItem:

    navigationController?.toolbarHidden = false
    let barButtonItem = MKUserTrackingBarButtonItem(mapView: self.mapView)
    self.toolbarItems = [barButtonItem]
    
    0 讨论(0)
  • 2021-02-04 03:42

    Updated answer using the current selector syntax for

    Swift 3

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

    You can place this code in any loading event. It works seamless for me in viewDidLoad().

    Replace "ThisViewController.onDoneBarButtonClick" with your view controller class name and any method you want to write to manage the toolbar button click.

    0 讨论(0)
提交回复
热议问题