UITextFieldのキーボードにUIToolbarを追加する

UITextFieldのキーボードに下記のようなUIToolbarを実装してみたいと思います。安易に実装が出来て非常に便利だと思ったので書き残しておきます。

# 実行環境

- xcode8
- swift3

# 元ネタ

![no_one.gif](https://qiita-image-store.s3.amazonaws.com/0/135794/25c0bf5f-97b1-9949-8a76-a728f6620bcb.gif)

```swift
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
    }
}
```

# 実装

![no2.gif](https://qiita-image-store.s3.amazonaws.com/0/135794/eaf445e9-2d50-d481-77cd-94e242a387ea.gif)

``` swift

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("ドレイクはリアーナが大好き")
    }
}

```


# 参考

[Add UIToolBar to all keyboards (swift)](https://stackoverflow.com/questions/30983516/add-uitoolbar-to-all-keyboards-swift)

[UITextFieldのキーボードにUIToolbarを追加する](http://off.tokyo/blog/2017/06/16/uitextfield%E3%81%AE%E3%82%AD%E3%83%BC%E3%83%9C%E3%83%BC%E3%83%89%E3%81%ABuitoolbar%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B/)

# ソース

[UITextFieldのキーボードにUIToolbarを追加する](https://github.com/Ryosuke-Hujisawa/Swift3-ToolBarUITextFieldTutorial)