This code I created translating from Objective-C to Swift worked for me.
I'm not using scrollView
and instead of moving textView.frame
, I'm using contentInset
to crawl 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 think I'm pretty close to what we're looking for, it worked for me in Xcode 7 beta 4, I hope it helps.