Adding buttons to toolbar programmatically in swift

前端 未结 6 1651
天命终不由人
天命终不由人 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: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.

提交回复
热议问题