
UITextFieldのキーボードにUIToolbarを追加する
UITextFieldのキーボードに下記のようなUIToolbarを実装してみたいと思います。二個目の実装は参照を参考に改造をしたものです。安易に実装が出来て非常に便利だと思ったので書き残しておきます。
元ネタ
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var addressField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
addToolBar(textField: addressField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension UIViewController: UITextFieldDelegate{
func addToolBar(textField: UITextField){
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(UIViewController.donePressed))
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(UIViewController.cancelPressed))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
textField.delegate = self
textField.inputAccessoryView = toolBar
}
func donePressed(){
view.endEditing(true)
}
func cancelPressed(){
view.endEditing(true) // or do something
}
}
実装
実装
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var addressField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
addToolBar(textField: addressField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension UIViewController: UITextFieldDelegate{
func addToolBar(textField: UITextField){
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
let JayZButton = UIBarButtonItem(title: "JayZ", style: UIBarButtonItemStyle.done, target: self, action: #selector(UIViewController.RapGod))
let BeyoncéButton = UIBarButtonItem(title: "Beyoncé", style: UIBarButtonItemStyle.plain, target: self, action: #selector(UIViewController.JayZwife))
let DrakeButton = UIBarButtonItem(title: "Drake", style: UIBarButtonItemStyle.plain, target: self, action: #selector(UIViewController.bestIeverHad))
let RihannaButton = UIBarButtonItem(title: "Rihanna", style: UIBarButtonItemStyle.plain, target: self, action: #selector(UIViewController.DrakeLoveRihanna))
//let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
toolBar.setItems([JayZButton, BeyoncéButton, DrakeButton, RihannaButton], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
textField.delegate = self
textField.inputAccessoryView = toolBar
}
func RapGod(){
// view.endEditing(true)
print("ラップの神様")
}
func JayZwife(){
// view.endEditing(true)
print("JayZの嫁")
}
func bestIeverHad(){
print("bestIeverHad")
}
func DrakeLoveRihanna(){
print("ドレイクはリアーナが大好き")
}
}