Skip to content

Instantly share code, notes, and snippets.

@macu
Last active September 9, 2015 00:24
Show Gist options
  • Select an option

  • Save macu/e2408971f7f67678eda2 to your computer and use it in GitHub Desktop.

Select an option

Save macu/e2408971f7f67678eda2 to your computer and use it in GitHub Desktop.
WIP and has a bug
// ...
override func viewDidLoad()
{
super.viewDidLoad()
// Thanks http://stackoverflow.com/a/27665207
NSNotificationCenter.defaultCenter().addObserverForName(
UIKeyboardWillChangeFrameNotification,
object: nil,
queue: nil,
usingBlock: { notification in
let userInfo = notification.userInfo!
let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue
let animationCurve = userInfo[UIKeyboardAnimationCurveUserInfoKey]!.unsignedIntValue
let frameEnd = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue()
let convertedFrameEnd = self.view.convertRect(frameEnd, fromView: nil)
let keyboardChopsBottom = max(0, self.view.frame.height - convertedFrameEnd.origin.y)
println("keyboard covers bottom \(keyboardChopsBottom) of view")
UIView.animateWithDuration(
animationDuration,
delay: 0,
options: UIViewAnimationOptions(UInt(animationCurve)),
animations: {
self.view.layoutIfNeeded()
},
completion: nil
)
}
)
}
override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self)
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment