Reputation: 169
I have been searching for ages for a way to set the height of my textview to the leftover space after the keyboard appears. The keyboard never hides and is up permanently.
How am I able to place the textview in the space at the top of the screen when the keyboard is up.
I am desperate for an answer as nothing I have found has worked, and I also need it in Swift, not Objective-C as I think one of the reasons I haven't been able to get it working is because I have been translating the code wrong.
Thanks!
Upvotes: 0
Views: 953
Reputation: 1849
This is how you find the height of the onscreen keyboard: onscreen keyboard height stackoverflow
next I assume your textView is covering the whole view of parent viewController.
make your viewController conform to UITextViewDelegate and implement this method ->
func textViewDidBeginEditing(textView: UITextView) -> Bool{
textView.bounds.size.height = view.bounds.size.height - KEYBOARDHEIGHT(method from that link of stackoverflow)
}
also in the viewDidLoad :
yourTextView.delegate = self
Upvotes: 1
Reputation: 169
Finally found a solution.
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
textView.contentInset = contentInsets
}
}
Upvotes: 1