Skip to content

Instantly share code, notes, and snippets.

@Winchariot
Last active November 22, 2023 14:26
Show Gist options
  • Save Winchariot/36b582625d1d2b659636dce76e27793a to your computer and use it in GitHub Desktop.
Save Winchariot/36b582625d1d2b659636dce76e27793a to your computer and use it in GitHub Desktop.
UIToolbar with a right-aligned "Done" button, for use as a keyboard accessory view
lazy var accessoryToolbarWithDoneButton: UIToolbar = {
let toolbar = UIToolbar(frame: CGRect.zero)
//only needs to be flexible in the dimension(s) you want it to be
//https://stackoverflow.com/questions/31822504/how-can-i-increase-the-height-of-an-inputaccessoryview
toolbar.autoresizingMask = [.flexibleWidth, .flexibleHeight]
let leftSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(keyboardDoneButtonTapped(_:)))
toolbar.items = [leftSpace, doneButton]
return toolbar
}()
@objc func keyboardDoneButtonTapped(_ sender: UIBarButtonItem) {
//resign first responder on the appropriate field
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment