Skip to content

Instantly share code, notes, and snippets.

@kingiol
Created November 10, 2015 11:27
Show Gist options
  • Save kingiol/a423242b52181633f8b3 to your computer and use it in GitHub Desktop.
Save kingiol/a423242b52181633f8b3 to your computer and use it in GitHub Desktop.
iOS键盘事件
【iOS中有关键盘事件】
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "adjustForKeyboard:", name: UIKeyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: "adjustForKeyboard:", name: UIKeyboardWillChangeFrameNotification, object: nil)
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 {
script.contentInset = UIEdgeInsetsZero
}else {
script.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
}
script.scrollIndicatorInsets = script.contentInset
let selectedRange = script.selectedRange
script.scrollRangeToVisible(selectedRange)
}
PS: 其中script为UITextView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment