Created
August 23, 2018 20:36
-
-
Save jingzhehu/cccd695dcafa94425d6c879aa304651c to your computer and use it in GitHub Desktop.
ScrollView unhide content with keyboard
This file contains hidden or 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
@IBOutlet weak var scrollView: UIScrollView! | |
func registerForKeyboardNotifications() { | |
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: .UIKeyboardDidShow, object: nil) | |
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil) | |
} | |
@IBAction func keyboardWasShown(_ notification: NSNotification) { | |
guard let userInfo = notification.userInfo, | |
let keyboardFrameValue = userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue | |
else {return } | |
let keyboardHeight = keyboardFrameValue.cgRectValue.size.height | |
let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight+50, 0.0) | |
scrollView.contentInset = contentInsets | |
scrollView.scrollIndicatorInsets = contentInsets | |
} | |
@IBAction func keyboardWillBeHidden(_ notification: NSNotification) { | |
let contentInsets = UIEdgeInsets.zero | |
scrollView.contentInset = contentInsets | |
scrollView.scrollIndicatorInsets = contentInsets | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment