Skip to content

Instantly share code, notes, and snippets.

@hanishassim
Forked from louisdh/NYSKeyboardHelper.swift
Created September 20, 2018 14:39
Show Gist options
  • Select an option

  • Save hanishassim/d5ddc5eb1c752d041df42f75f346d6a3 to your computer and use it in GitHub Desktop.

Select an option

Save hanishassim/d5ddc5eb1c752d041df42f75f346d6a3 to your computer and use it in GitHub Desktop.

Revisions

  1. @louisdh louisdh revised this gist Jun 26, 2017. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions NYSKeyboardHelper.swift
    Original file line number Diff line number Diff line change
    @@ -85,11 +85,11 @@ extension UIViewAnimationOptions {
    case .easeIn:
    self = .curveEaseIn
    case .easeOut:
    self = .curveEaseOut
    case .easeInOut:
    self = .curveEaseInOut
    case .linear:
    self = .curveLinear
    }
    }
    self = .curveEaseOut
    case .easeInOut:
    self = .curveEaseInOut
    case .linear:
    self = .curveLinear
    }
    }
    }
  2. @louisdh louisdh revised this gist Jun 26, 2017. No changes.
  3. @louisdh louisdh revised this gist Jun 26, 2017. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions NYSKeyboardHelper.swift
    Original file line number Diff line number Diff line change
    @@ -80,11 +80,11 @@ class NYSKeyboardHelper: NSLayoutConstraint {
    }

    extension UIViewAnimationOptions {
    init(curve: UIViewAnimationCurve) {
    switch curve {
    case .easeIn:
    self = .curveEaseIn
    case .easeOut:
    init(curve: UIViewAnimationCurve) {
    switch curve {
    case .easeIn:
    self = .curveEaseIn
    case .easeOut:
    self = .curveEaseOut
    case .easeInOut:
    self = .curveEaseInOut
  4. @louisdh louisdh revised this gist Jun 26, 2017. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions NYSKeyboardHelper.swift
    Original file line number Diff line number Diff line change
    @@ -16,11 +16,11 @@ class NYSKeyboardHelper: NSLayoutConstraint {
    setup()
    }

    override func awakeFromNib() {
    super.awakeFromNib()
    override func awakeFromNib() {
    super.awakeFromNib()

    setup()
    }
    setup()
    }

    private func setup() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidChangeVisible(notification:)), name: .UIKeyboardWillHide, object: nil)
    @@ -46,7 +46,7 @@ class NYSKeyboardHelper: NSLayoutConstraint {
    return
    }

    let userInfo = notification.userInfo
    let userInfo = notification.userInfo

    var animationDuration: TimeInterval = 0.0
    var animationOptions: UIViewAnimationOptions = []
    @@ -69,7 +69,7 @@ class NYSKeyboardHelper: NSLayoutConstraint {
    let offset = contentAbsoluteFrame.maxY - endFrame.minY + extraIndent
    let keyboardHeight = max(0, offset)

    animationOptions.update(with: .layoutSubviews)
    animationOptions.update(with: .layoutSubviews)

    UIView .animate(withDuration: animationDuration, delay: 0.0, options: animationOptions, animations: {
    self.constant = keyboardHeight
  5. @louisdh louisdh revised this gist Jun 26, 2017. 1 changed file with 37 additions and 13 deletions.
    50 changes: 37 additions & 13 deletions NYSKeyboardHelper.swift
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    // NYSKeyboardHelper.swift
    // B-NYS GCV
    //
    // Created by Matthias on 18/03/2017.
    // Created by Matthias Nys on 18/03/2017.
    // Copyright © 2017 B-NYS. All rights reserved.
    //

    @@ -15,7 +15,13 @@ class NYSKeyboardHelper: NSLayoutConstraint {
    super.init()
    setup()
    }


    override func awakeFromNib() {
    super.awakeFromNib()

    setup()
    }

    private func setup() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidChangeVisible(notification:)), name: .UIKeyboardWillHide, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidChangeVisible(notification:)), name: .UIKeyboardWillShow, object: nil)
    @@ -39,33 +45,51 @@ class NYSKeyboardHelper: NSLayoutConstraint {
    print("NYSKEYBOARDHELPER: Fist item needs a superview of the type UIView")
    return
    }


    let userInfo = notification.userInfo

    var animationDuration: TimeInterval = 0.0
    var animationOptions: UIViewAnimationOptions = UIViewAnimationOptions(rawValue: 0)
    var animationOptions: UIViewAnimationOptions = []

    var endFrame = CGRect.zero

    if let animationCurve = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? UIViewAnimationCurve {
    animationOptions = UIViewAnimationOptions(rawValue: UInt(animationCurve.rawValue << 16))
    if let animationCurve = userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? UIViewAnimationCurve {
    animationOptions = UIViewAnimationOptions(curve: animationCurve)
    }

    if let animationDurationFromUserInfo = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval {
    if let animationDurationFromUserInfo = userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval {
    animationDuration = animationDurationFromUserInfo
    }

    if let endFrameFromUserInfo = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect {
    if let endFrameFromUserInfo = userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect {
    endFrame = endFrameFromUserInfo
    }

    let contentAbsoluteFrame = superview.convert(firstItem.frame, to: nil) //To nil -> On the Window
    let offset = contentAbsoluteFrame.maxY - endFrame.minY + extraIndent
    let keyboardHeight = max(0, offset)
    animationOptions = UIViewAnimationOptions(rawValue: animationOptions.rawValue | UIViewAnimationOptions.layoutSubviews.rawValue)
    animationOptions.update(with: .layoutSubviews)
    UIView .animate(withDuration: animationDuration, delay: 0.0, options: animationOptions, animations: {
    self.constant = keyboardHeight
    firstItem.layoutIfNeeded()
    }, completion: nil)
    }
    }
    }

    extension UIViewAnimationOptions {
    init(curve: UIViewAnimationCurve) {
    switch curve {
    case .easeIn:
    self = .curveEaseIn
    case .easeOut:
    self = .curveEaseOut
    case .easeInOut:
    self = .curveEaseInOut
    case .linear:
    self = .curveLinear
    }
    }
    }
  6. @matthiasnys matthiasnys created this gist May 26, 2017.
    71 changes: 71 additions & 0 deletions NYSKeyboardHelper.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    //
    // NYSKeyboardHelper.swift
    // B-NYS GCV
    //
    // Created by Matthias on 18/03/2017.
    // Copyright © 2017 B-NYS. All rights reserved.
    //

    import UIKit

    class NYSKeyboardHelper: NSLayoutConstraint {
    @IBInspectable var extraIndent: CGFloat = 0.0

    override init() {
    super.init()
    setup()
    }

    private func setup() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidChangeVisible(notification:)), name: .UIKeyboardWillHide, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidChangeVisible(notification:)), name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidChangeVisible(notification:)), name: .UIKeyboardDidShow, object: nil)
    }

    deinit {
    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: .UIKeyboardDidShow, object: nil)
    }

    @objc private func keyboardDidChangeVisible(notification: Notification) {

    guard let firstItem = self.firstItem as? UIView else {
    print("NYSKEYBOARDHELPER: Fist item needs to be a UIView")
    return
    }

    guard let superview = firstItem.superview else {
    print("NYSKEYBOARDHELPER: Fist item needs a superview of the type UIView")
    return
    }

    var animationDuration: TimeInterval = 0.0
    var animationOptions: UIViewAnimationOptions = UIViewAnimationOptions(rawValue: 0)
    var endFrame = CGRect.zero

    if let animationCurve = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? UIViewAnimationCurve {
    animationOptions = UIViewAnimationOptions(rawValue: UInt(animationCurve.rawValue << 16))
    }

    if let animationDurationFromUserInfo = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval {
    animationDuration = animationDurationFromUserInfo
    }

    if let endFrameFromUserInfo = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect {
    endFrame = endFrameFromUserInfo
    }

    let contentAbsoluteFrame = superview.convert(firstItem.frame, to: nil) //To nil -> On the Window
    let offset = contentAbsoluteFrame.maxY - endFrame.minY + extraIndent
    let keyboardHeight = max(0, offset)

    animationOptions = UIViewAnimationOptions(rawValue: animationOptions.rawValue | UIViewAnimationOptions.layoutSubviews.rawValue)

    UIView .animate(withDuration: animationDuration, delay: 0.0, options: animationOptions, animations: {
    self.constant = keyboardHeight
    firstItem.layoutIfNeeded()
    }, completion: nil)

    }
    }