LoginSignup
10
12

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-06-16

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

実行環境

  • xcode8
  • swift3

元ネタ

no_one.gif

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


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)

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

ソース

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

10
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
12