Created
November 10, 2015 11:27
-
-
Save kingiol/a423242b52181633f8b3 to your computer and use it in GitHub Desktop.
iOS键盘事件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
【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