Adding buttons to toolbar programmatically in swift

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

提交回复
热议问题