Adding buttons to toolbar programmatically in swift

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

提交回复
热议问题