Reputation: 531
I'm refactoring the view controller to include the feature to dismiss the keyboard. The layout has a UITextView and a toolbar (there is no off screen area that the user to dismiss the keyboard) see below -
In the storyboard, there is an option to dismiss keyboard in different ways in the UITextView and I changed it to dismiss on drag, or dismiss interactively. I don't see any change and couldn't do anything to dismiss the keyboard. How can I get the keyboard to dismiss interactively with the UITextView?
Upvotes: 3
Views: 1660
Reputation: 19
You can customize your keyboard and add a button for hide keyboard. After that, when you tap to screen, keyboard will appear and you can write again. Here is my sample code:
class ViewController: UIViewController {
@IBOutlet weak var myTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
createToolbar(textField: myTextView)
}
func createToolbar(textField : UITextView) {
let toolbar = UIToolbar()
toolbar.barStyle = UIBarStyle.default
toolbar.sizeToFit()
let hidekeyboard = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(hidekeyboardd))
toolbar.items = [hidekeyboard]
myTextView.inputAccessoryView = toolbar
}
@objc func hidekeyboardd() {
myTextView.resignFirstResponder()
}
Upvotes: 2
Reputation: 15331
For the built-in keyboard dismissing to work for a UIScollView
or one of its subclasses (e.g. UITextView
) the scroll view needs to be able to scroll. If there's not enough text in the text view to provide for scrolling, it won't perform a keyboard dismiss.
However, you can turn on vertical bouncing and then it will work. Check "Bounce Vertically" in interface builder or in code set myTextView.alwaysBounceVertical = true
.
Upvotes: 5