iosiphoneiphone-sdk-3.0uitextviewiphone-softkeyboard

Hide Virtual Keyboard of UITextView when 'Done' Presses


I want to hide (resignFirstResponder) the virtual keyboard of UITextView when 'Done' presses. Theres no 'Did End on Exit' in UITextView. In UITextField i connect the 'Did End on Exit' with an IBAction and call resignFirstResponder method. How can i do this with UITextView?


Solution

  • Here is the Swift version of the accessory "Done" button:

    @IBOutlet weak var textView: UITextView!
    
    // In viewDidLoad()
    
        let toolbar = UIToolbar()
        toolbar.bounds = CGRectMake(0, 0, 320, 50)
        toolbar.sizeToFit()
        toolbar.barStyle = UIBarStyle.Default
        toolbar.items = [
            UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
            UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: nil, action: "handleDone:")
        ]
    
        self.textView.inputAccessoryView = toolbar
    
    // -----------------
    
    func handleDone(sender:UIButton) {
        self.textView.resignFirstResponder()
    }