Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions KeyboardHelper/Classes/KeyboardHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,34 @@ import UIKit

/// Protocol `KeyboardHelperDelegate` requires two functions, `keyboardWillAppear` and `keyboardWillDisappear` with parameter `info` struct `KeyboardAppearanceInfo`.
public protocol KeyboardHelperDelegate: class {


/// This function will recongnize a change of `KeyboardAppearanceInfo` and will be fired when the keyboard will appaear.
///
/// - Parameter info: Struct `KeyboardAppearanceInfo`.
func keyboardWillAppear(_ info: KeyboardAppearanceInfo)


/// This function will recongnize a change of `KeyboardAppearanceInfo` and will be fired when the keyboard will disappaear.
///
/// - Parameter info: Struct `KeyboardAppearanceInfo`.
func keyboardWillDisappear(_ info: KeyboardAppearanceInfo)
}

extension KeyboardHelperDelegate {
/// This function will recongnize a change of `KeyboardAppearanceInfo` and will be fired when the keyboard will appaear.
///
/// - Parameter info: Struct `KeyboardAppearanceInfo`.
func keyboardWillAppear(_ info: KeyboardAppearanceInfo) {
// default empty implementation
}


/// This function will recongnize a change of `KeyboardAppearanceInfo` and will be fired when the keyboard will disappaear.
///
/// - Parameter info: Struct `KeyboardAppearanceInfo`.
func keyboardWillDisappear(_ info: KeyboardAppearanceInfo) {
// default empty implementation
}
}

/// Useful helper to keep track of keyboard changes.
public class KeyboardHelper {
Expand Down
1 change: 0 additions & 1 deletion Sources/KeyboardHelper.swift

This file was deleted.

72 changes: 72 additions & 0 deletions Sources/KeyboardHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// KeyboardHelper.swift
// KeyboardHelper
//
// Created by Kasper Welner on 27/01/16.
// Copyright © 2016 Nodes. All rights reserved.
//

import Foundation
import UIKit


/// Protocol `KeyboardHelperDelegate` requires two functions, `keyboardWillAppear` and `keyboardWillDisappear` with parameter `info` struct `KeyboardAppearanceInfo`.
public protocol KeyboardHelperDelegate: class {


/// This function will recongnize a change of `KeyboardAppearanceInfo` and will be fired when the keyboard will appaear.
///
/// - Parameter info: Struct `KeyboardAppearanceInfo`.
func keyboardWillAppear(_ info: KeyboardAppearanceInfo)


/// This function will recongnize a change of `KeyboardAppearanceInfo` and will be fired when the keyboard will disappaear.
///
/// - Parameter info: Struct `KeyboardAppearanceInfo`.
func keyboardWillDisappear(_ info: KeyboardAppearanceInfo)
}

extension KeyboardHelperDelegate {

}

/// Useful helper to keep track of keyboard changes.
public class KeyboardHelper {

/// Delegate that conforms with the `KeyboardHelperDelegate`.
public weak var delegate: KeyboardHelperDelegate?

/// Initialize the `delegate` and add the two observer for `keyboardWillAppear` and `keyboardWillDisappear`.
/// Observers are nessecary for tracking the `UIKeyboardWillShowNotification` and `UIKeyboardWillHideNotification`, so the function that are connectet are getting fired.
///
/// - Parameter delegate: KeyboardHelperDelegate
required public init(delegate: KeyboardHelperDelegate) {
self.delegate = delegate

NotificationCenter.default.addObserver(self, selector: #selector(KeyboardHelper.keyboardWillAppear(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(KeyboardHelper.keyboardWillDisappear(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}


/// Making sure that you can't intantiate it without a delegate
private init() {
delegate = nil
}

dynamic private func keyboardWillAppear(_ note: Notification) {
let info = KeyboardAppearanceInfo(notification: note)
self.delegate?.keyboardWillAppear(info)
}

dynamic private func keyboardWillDisappear(_ note: Notification) {
let info = KeyboardAppearanceInfo(notification: note)
self.delegate?.keyboardWillDisappear(info)
}

deinit {
NotificationCenter.default.removeObserver(self)
}
}

@available(*, deprecated, message: "KeyboardNotificationDelegate has been renamed to KeyboardHelperDelegate")
public typealias KeyboardNotificationDelegate = KeyboardHelperDelegate