UITextField 키보드에 UIToolbar 추가

UITextField 키보드에 아래와 같은 UIToolbar를 구현하고 싶습니다. 안이하게 실장이 되어 매우 편리하다고 생각했으므로 써 남겨 둡니다.

실행 환경


  • xcode8
  • swift3

  • 전 자료




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

    참고



    Add UIToolBar to all keyboards (swift)

    UITextField 키보드에 UIToolbar 추가

    출처



    UITextField 키보드에 UIToolbar 추가

    좋은 웹페이지 즐겨찾기