0

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!

zacreme
  • 169
  • 1
  • 12

2 Answers2

1

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
    }
}
zacreme
  • 169
  • 1
  • 12
1

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
Community
  • 1
  • 1
IamMashed
  • 1,811
  • 2
  • 21
  • 32