Swift textfield keyboard 에 Done 버튼 생성하기
import Foundation
import UIKit
extension UITextField{
@IBInspectable var doneAccessory: Bool {
get {
return self.doneAccessory
}
set (hasDone) {
if hasDone {
addDoneButtonOnKeyboard()
}
}
}
func addDoneButtonOnKeyboard(){
let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x:0,y:0,width: UIScreen.main.bounds.width,height: 50))
doneToolbar.barStyle = .default
doneToolbar.tintColor = .green
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let done: UIBarButtonItem = UIBarButtonItem(title: "키워드 등록", style: .done, target: self, action: #selector(doneButtonAction))
let items = [flexSpace,done]
doneToolbar.items = items
doneToolbar.sizeToFit()
self.inputAccessoryView = doneToolbar
}
@objc func doneButtonAction() {
self.resignFirstResponder()
}
}