Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. override func viewWillAppear(animated: Bool) {
  2. super.viewWillAppear(animated)
  3.  
  4. NSNotificationCenter.defaultCenter().addObserver(self,
  5. selector: "keyboardWillShow:",
  6. name: UIKeyboardWillShowNotification,
  7. object: nil)
  8. NSNotificationCenter.defaultCenter().addObserver(self,
  9. selector: "keyboardWillHide:",
  10. name: UIKeyboardWillHideNotification,
  11. object: nil)
  12. }
  13.  
  14. override func viewWillDisappear(animated: Bool) {
  15. NSNotificationCenter.defaultCenter().removeObserver(self,
  16. name: UIKeyboardWillShowNotification,
  17. object: nil)
  18. NSNotificationCenter.defaultCenter().removeObserver(self,
  19. name: UIKeyboardWillHideNotification,
  20. object: nil)
  21.  
  22. super.viewWillDisappear(animated)
  23. }
  24.  
  25. func keyboardWillShow(notification: NSNotification) {
  26. if let userInfo = notification.userInfo {
  27. let value = userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue
  28. let rect = value.CGRectValue()
  29.  
  30. UIView.animateWithDuration(0.3,
  31. animations: { [unowned self] () -> Void in
  32. var frame = self.view.frame
  33. frame.origin.y = -(rect.origin.y / 4)
  34. self.view.frame = frame
  35. })
  36. }
  37. }
  38.  
  39. func keyboardWillHide(notification: NSNotification) {
  40. UIView.animateWithDuration(0.4,
  41. animations: { [unowned self] () -> Void in
  42. var frame = self.view.frame
  43. frame.origin.y = 0.0
  44. self.view.frame = frame
  45. })
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement