4

I m using UITableView with 2 customs cells, I have custom cells with UITextView and UITextField, I'm trying to move up the edited field when it's hiding by the keyboard on the top of the keyboard, here is my code for the viewDidLoad :

 override func viewDidLoad() {
        super.viewDidLoad()
 let notificationCenter = NSNotificationCenter.defaultCenter()
        notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillShowNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillHideNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillChangeFrameNotification, object: nil)

    }

and here the function called when keyboard notification is sent :

func adjustForKeyboard(notification: NSNotification) {
    let userInfo = notification.userInfo!

    let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)

    if notification.name == UIKeyboardWillHideNotification {
        myTableView.contentInset = UIEdgeInsetsZero
        print("ZERO")
    } else {
        myTableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
    }

    myTableView.scrollIndicatorInsets = myTableView.contentInset
}

It's working perfectly for the UITextField but not for the UITextView. Why?

stevekohls
  • 2,214
  • 23
  • 29
fandro
  • 4,833
  • 7
  • 41
  • 62

1 Answers1

1

As answered by Steve (dismiss keyboard with a uiTextView)

import UIKit

class ViewController: UIViewController, UITextViewDelegate {

    @IBOutlet weak var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        textView.delegate = self
    }

    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        if(text == "\n") {
            textView.resignFirstResponder()
            return false
        }
        return true
    }

}
Community
  • 1
  • 1
Albert Ghar
  • 428
  • 1
  • 6
  • 14