1
This code I created translating from Objective-C to Swift worked for me.
I am not using scrollView
and instead of moving the textView.frame
, I’m using contentInset
to track the cursor.
override func viewDidLoad() {
registerForKeyboardNotification()
textView.delegate = self
}
func registerForKeyboardNotification() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWasShown:"), name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillBeHidden:"), name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWasShown(aNotification: NSNotification) {
let info = aNotification.userInfo!
let kbSize = (info[UIKeyboardFrameBeginUserInfoKey])!.CGRectValue.size
let contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0)
textView.contentInset = contentInsets
textView.scrollIndicatorInsets = contentInsets
}
func keyboardWillBeHidden(aNotification: NSNotification) {
let contentInsets = UIEdgeInsetsZero
textView.contentInset = contentInsets
textView.scrollIndicatorInsets = contentInsets
textView.resignFirstResponder()
}
I believe to be very close to what we look for, it worked for me in Xcode 7 beta 4, I hope it helps.