2.IOS(swift)-scrollView ·Keyboar
2015-03-05 本文已影响701人
俊瑶先森
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
```
```
deinit{
NSNotificationCenter.defaultCenter().removeObserver(self)
}
```
```
func keyboardWillShow(notification: NSNotification) {
adjustInsetForKeyboardShow(true, notification: notification)
}
func keyboardWillHide(notification: NSNotification) {
adjustInsetForKeyboardShow(false, notification: notification)
}
func adjustInsetForKeyboardShow(show:Bool,notification:NSNotification){
let userInfo = notification.userInfo ?? [:]
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as NSValue).CGRectValue()
let adjustmentHeight = (CGRectGetHeight(keyboardFrame)) * (show ? 1:-1)
fgScrollview.contentInset.bottom += adjustmentHeight
fgScrollview.scrollIndicatorInsets.bottom += adjustmentHeight
}
```