KeyboardResizeCenterY.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. class KeyboardResizeCenterY {
  22. let parentView: UIView
  23. let resizeView: UIView
  24. var defaultCenterY: NSLayoutConstraint
  25. var offsetCenterY: NSLayoutConstraint?
  26. init(parent: UIView, resize: UIView) {
  27. self.parentView = parent
  28. self.resizeView = resize
  29. self.defaultCenterY = NSLayoutConstraint(item: self.resizeView, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.parentView, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1.0, constant: 0)
  30. self.parentView.addConstraint(self.defaultCenterY)
  31. NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
  32. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
  33. }
  34. deinit {
  35. NotificationCenter.default.removeObserver(self)
  36. }
  37. @objc func keyboardDidShow(notification: NSNotification) {
  38. if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
  39. let offset: CGFloat = UIScreen.main.bounds.height - self.resizeView.frame.maxY - keyboardSize.height - 10
  40. if offset < 0 {
  41. self.offsetCenterY = NSLayoutConstraint(item: self.resizeView, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.parentView, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1.0, constant: offset)
  42. self.parentView.removeConstraint(self.defaultCenterY)
  43. self.parentView.addConstraint(self.offsetCenterY!)
  44. }
  45. }
  46. }
  47. @objc func keyboardWillHide(notification: NSNotification) {
  48. if let offsetCenterY = self.offsetCenterY {
  49. self.parentView.removeConstraint(offsetCenterY)
  50. }
  51. self.parentView.addConstraint(self.defaultCenterY)
  52. }
  53. }