Skip to content

Instantly share code, notes, and snippets.

@nQaze
Created April 30, 2020 08:49
Show Gist options
  • Save nQaze/2674ebb3202d95f7f94e439a4700d42a to your computer and use it in GitHub Desktop.
Save nQaze/2674ebb3202d95f7f94e439a4700d42a to your computer and use it in GitHub Desktop.
class ViewController: UIViewController {
@IBOutlet weak var emailTF: UnderlinedTextField!
@IBOutlet weak var passwordTF: UnderlinedTextField!
@IBOutlet weak var backgroundSV: UIScrollView!
static func storyboardInstance() -> Solution3VC? {
let storyboard = UIStoryboard(name: Storyboard.main ,bundle: nil)
return storyboard.instantiateViewController(withIdentifier: "Solution3VC") as? Solution3VC
}
override func viewDidLoad() {
super.viewDidLoad()
//Subscribe to a Notification which will fire before the keyboard will show
subscribeToNotification(UIResponder.keyboardWillShowNotification, selector: #selector(keyboardWillShowOrHide))
//Subscribe to a Notification which will fire before the keyboard will hide
subscribeToNotification(UIResponder.keyboardWillHideNotification, selector: #selector(keyboardWillShowOrHide))
//We make a call to our keyboard handling function as soon as the view is loaded.
initializeHideKeyboard()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
//Unsubscribe from all our notifications
unsubscribeFromAllNotifications()
}
}
extension ViewController {
func initializeHideKeyboard(){
//Declare a Tap Gesture Recognizer which will trigger our dismissMyKeyboard() function
let tap: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(dismissMyKeyboard))
//Add this tap gesture recognizer to the parent view
view.addGestureRecognizer(tap)
}
@objc func dismissMyKeyboard(){
//endEditing causes the view (or one of its embedded text fields) to resign the first responder status.
//In short- Dismiss the active keyboard.
view.endEditing(true)
}
}
extension ViewController {
func subscribeToNotification(_ notification: NSNotification.Name, selector: Selector) {
NotificationCenter.default.addObserver(self, selector: selector, name: notification, object: nil)
}
func unsubscribeFromAllNotifications() {
NotificationCenter.default.removeObserver(self)
}
@objc func keyboardWillShowOrHide(notification: NSNotification) {
// Get required info out of the notification
if let scrollView = backgroundSV, let userInfo = notification.userInfo, let endValue = userInfo[UIResponder.keyboardFrameEndUserInfoKey], let durationValue = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey], let curveValue = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] {
// Transform the keyboard's frame into our view's coordinate system
let endRect = view.convert((endValue as AnyObject).cgRectValue, from: view.window)
// Find out how much the keyboard overlaps our scroll view
let keyboardOverlap = scrollView.frame.maxY - endRect.origin.y
// Set the scroll view's content inset & scroll indicator to avoid the keyboard
scrollView.contentInset.bottom = keyboardOverlap
scrollView.scrollIndicatorInsets.bottom = keyboardOverlap
let duration = (durationValue as AnyObject).doubleValue
let options = UIView.AnimationOptions(rawValue: UInt((curveValue as AnyObject).integerValue << 16))
UIView.animate(withDuration: duration!, delay: 0, options: options, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment