Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active October 6, 2023 14:45
Show Gist options
  • Save kristopherjohnson/13d5f18b0d56b0ea9242 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/13d5f18b0d56b0ea9242 to your computer and use it in GitHub Desktop.

Revisions

  1. kristopherjohnson revised this gist Apr 25, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion KeyboardNotification.swift
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ import UIKit

    /// Wrapper for the NSNotification userInfo values associated with a keyboard notification.
    ///
    /// It provides properties retrieve userInfo dictionary values with these keys:
    /// It provides properties that retrieve userInfo dictionary values with these keys:
    ///
    /// - UIKeyboardFrameBeginUserInfoKey
    /// - UIKeyboardFrameEndUserInfoKey
  2. kristopherjohnson revised this gist Apr 25, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion KeyboardNotification.swift
    Original file line number Diff line number Diff line change
    @@ -77,7 +77,7 @@ public struct KeyboardNotification {
    return view.convertRect(screenFrameBegin, fromView: view.window)
    }

    /// Start frame of the keyboard in coordinates of specified view
    /// End frame of the keyboard in coordinates of specified view
    ///
    /// :param: view UIView to whose coordinate system the frame will be converted
    /// :returns: frame rectangle in view's coordinate system
  3. kristopherjohnson revised this gist Dec 31, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ViewController.swift
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ final class ViewController: UIViewController {
    UIView.animateWithDuration(animationDuration,
    delay: 0,
    options: UIViewAnimationOptions(rawValue: UInt(animationCurve << 16)),
    animations: { () -> Void in
    animations: {
    self.bottomLayoutConstraint.constant = newBottomOffset
    self.view.layoutIfNeeded()
    },
  4. kristopherjohnson revised this gist Dec 31, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ViewController.swift
    Original file line number Diff line number Diff line change
    @@ -34,11 +34,13 @@ final class ViewController: UIViewController {
    let viewFrame = self.view.frame
    let newBottomOffset = viewFrame.maxY - keyboardFrame.minY

    self.view.layoutIfNeeded()
    UIView.animateWithDuration(animationDuration,
    delay: 0,
    options: UIViewAnimationOptions(rawValue: UInt(animationCurve << 16)),
    animations: { () -> Void in
    self.bottomLayoutConstraint.constant = newBottomOffset
    self.view.layoutIfNeeded()
    },
    completion: nil
    )
  5. kristopherjohnson revised this gist Dec 27, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ViewController.swift
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ final class ViewController: UIViewController {
    //
    // This constraint will be updated when the keyboard appears, disappears,
    // or changes size.
    @IBOutlet weak var inputTextFieldBottomLayoutConstraint: NSLayoutConstraint!
    @IBOutlet weak var bottomLayoutConstraint: NSLayoutConstraint!

    override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    @@ -38,7 +38,7 @@ final class ViewController: UIViewController {
    delay: 0,
    options: UIViewAnimationOptions(rawValue: UInt(animationCurve << 16)),
    animations: { () -> Void in
    self.inputTextFieldBottomLayoutConstraint.constant = newBottomOffset
    self.bottomLayoutConstraint.constant = newBottomOffset
    },
    completion: nil
    )
  6. kristopherjohnson revised this gist Dec 27, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ViewController.swift
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,8 @@ final class ViewController: UIViewController {
    animations: { () -> Void in
    self.inputTextFieldBottomLayoutConstraint.constant = newBottomOffset
    },
    completion: nil)
    completion: nil
    )
    }
    }

  7. kristopherjohnson revised this gist Dec 27, 2014. 1 changed file with 46 additions and 0 deletions.
    46 changes: 46 additions & 0 deletions ViewController.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    // Example of using KeyboardNotification to update an NSLayoutConstraint

    import UIKit

    final class ViewController: UIViewController {

    // Outlet for a layout constraint that specifies distance from bottom of
    // a subview to the bottom of the view.
    //
    // This constraint will be updated when the keyboard appears, disappears,
    // or changes size.
    @IBOutlet weak var inputTextFieldBottomLayoutConstraint: NSLayoutConstraint!

    override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    NSNotificationCenter.defaultCenter().addObserver(self,
    selector: "keyboardWillChangeFrameNotification:",
    name: UIKeyboardWillChangeFrameNotification,
    object: nil)
    }

    override func viewDidDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self)
    super.viewDidDisappear(animated)
    }

    func keyboardWillChangeFrameNotification(notification: NSNotification) {
    let n = KeyboardNotification(notification)
    let keyboardFrame = n.frameEndForView(self.view)
    let animationDuration = n.animationDuration
    let animationCurve = n.animationCurve

    let viewFrame = self.view.frame
    let newBottomOffset = viewFrame.maxY - keyboardFrame.minY

    UIView.animateWithDuration(animationDuration,
    delay: 0,
    options: UIViewAnimationOptions(rawValue: UInt(animationCurve << 16)),
    animations: { () -> Void in
    self.inputTextFieldBottomLayoutConstraint.constant = newBottomOffset
    },
    completion: nil)
    }
    }

  8. kristopherjohnson revised this gist Dec 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion KeyboardNotification.swift
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ public struct KeyboardNotification {
    /// Initializer
    ///
    /// :param: notification Keyboard-related notification
    public init(notification: NSNotification) {
    public init(_ notification: NSNotification) {
    self.notification = notification
    if let userInfo = notification.userInfo {
    self.userInfo = userInfo
  9. kristopherjohnson revised this gist Dec 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion KeyboardNotification.swift
    Original file line number Diff line number Diff line change
    @@ -66,7 +66,7 @@ public struct KeyboardNotification {
    if let number = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber {
    return number.integerValue
    }
    return UIViewAnimationCurve.EaseInOut.toRaw()
    return UIViewAnimationCurve.EaseInOut.rawValue
    }

    /// Start frame of the keyboard in coordinates of specified view
  10. kristopherjohnson revised this gist Dec 27, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions KeyboardNotification.swift
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    import UIKit

    /// Wrapper for the NSNotification userInfo values associated with a keyboard notification.
    ///
    /// It provides properties retrieve userInfo dictionary values with these keys:
  11. kristopherjohnson revised this gist Sep 12, 2014. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion KeyboardNotification.swift
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,12 @@
    /// Wrapper for the userInfo values associated with a UIKeyboard notification
    /// Wrapper for the NSNotification userInfo values associated with a keyboard notification.
    ///
    /// It provides properties retrieve userInfo dictionary values with these keys:
    ///
    /// - UIKeyboardFrameBeginUserInfoKey
    /// - UIKeyboardFrameEndUserInfoKey
    /// - UIKeyboardAnimationDurationUserInfoKey
    /// - UIKeyboardAnimationCurveUserInfoKey

    public struct KeyboardNotification {

    let notification: NSNotification
  12. kristopherjohnson revised this gist Sep 12, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion KeyboardNotification.swift
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,8 @@ public struct KeyboardNotification {
    /// Keyboard animation curve
    ///
    /// Note that the value returned by this method may not correspond to a
    /// UIViewAnimationCurve enum value.
    /// UIViewAnimationCurve enum value. For example, in iOS 7 and iOS 8,
    /// this returns the value 7.
    public var animationCurve: Int {
    if let number = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber {
    return number.integerValue
  13. kristopherjohnson created this gist Sep 12, 2014.
    76 changes: 76 additions & 0 deletions KeyboardNotification.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    /// Wrapper for the userInfo values associated with a UIKeyboard notification
    public struct KeyboardNotification {

    let notification: NSNotification
    let userInfo: NSDictionary

    /// Initializer
    ///
    /// :param: notification Keyboard-related notification
    public init(notification: NSNotification) {
    self.notification = notification
    if let userInfo = notification.userInfo {
    self.userInfo = userInfo
    }
    else {
    self.userInfo = NSDictionary()
    }
    }

    /// Start frame of the keyboard in screen coordinates
    public var screenFrameBegin: CGRect {
    if let value = userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue {
    return value.CGRectValue()
    }
    else {
    return CGRectZero
    }
    }

    /// End frame of the keyboard in screen coordinates
    public var screenFrameEnd: CGRect {
    if let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue {
    return value.CGRectValue()
    }
    else {
    return CGRectZero
    }
    }

    /// Keyboard animation duration
    public var animationDuration: Double {
    if let number = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber {
    return number.doubleValue
    }
    else {
    return 0.25
    }
    }

    /// Keyboard animation curve
    ///
    /// Note that the value returned by this method may not correspond to a
    /// UIViewAnimationCurve enum value.
    public var animationCurve: Int {
    if let number = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber {
    return number.integerValue
    }
    return UIViewAnimationCurve.EaseInOut.toRaw()
    }

    /// Start frame of the keyboard in coordinates of specified view
    ///
    /// :param: view UIView to whose coordinate system the frame will be converted
    /// :returns: frame rectangle in view's coordinate system
    public func frameBeginForView(view: UIView) -> CGRect {
    return view.convertRect(screenFrameBegin, fromView: view.window)
    }

    /// Start frame of the keyboard in coordinates of specified view
    ///
    /// :param: view UIView to whose coordinate system the frame will be converted
    /// :returns: frame rectangle in view's coordinate system
    public func frameEndForView(view: UIView) -> CGRect {
    return view.convertRect(screenFrameEnd, fromView: view.window)
    }
    }