Add toolbar with done and cancell for datepicker iOS swift
Problem:
Want to create a date picker for textfield while tap show date picker with done and cancel buttons with action.
Solution:
let toolBar = UIToolbar()
toolBar.sizeToFit()
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(doneAction))
let cancelBtn = UIBarButtonItem(barButtonSystemItem: .cancel, target: nil, action: #selector(cancelAction))
let spaceBtn = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action:nil)
myTF.inputAccessoryView = toolBar
myTF.inputView = datePicker
// MARK: - Done action
@objc func doneAction() {
self.view.endEditing(true)
}
// MARK: - cancel action
@objc func cancelAction() {
self.view.endEditing(true)
}
Comments
Post a Comment