Skip to content

Instantly share code, notes, and snippets.

@OscarApeland
Created March 6, 2018 10:55
Show Gist options
  • Save OscarApeland/a03ce965082d3e8e958e37cc71f45379 to your computer and use it in GitHub Desktop.
Save OscarApeland/a03ce965082d3e8e958e37cc71f45379 to your computer and use it in GitHub Desktop.
How to offset and animate something that hovers above the keyboard with auto layout constraints
NotificationCenter.default.addObserver(forName: .UIKeyboardWillChangeFrame, object: nil, queue: .main) { [weak self] (notification) in
guard let strongSelf = self,
let targetFrame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect,
let animationCurve = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber,
let animationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber else {
return
}
strongSelf.view.layoutIfNeeded()
strongSelf.proceedButtonBottomConstraint.constant = -UIScreen.main.bounds.intersection(targetFrame).height
UIView.animate(withDuration: TimeInterval(truncating: animationDuration), delay: 0.0,
options: UIViewAnimationOptions(rawValue: UInt(truncating: animationCurve)),
animations: strongSelf.view.layoutIfNeeded,
completion: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment