Skip to content

Commit

Permalink
v 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriFox committed Feb 2, 2019
1 parent a3bc1f7 commit c553672
Show file tree
Hide file tree
Showing 10 changed files with 787 additions and 0 deletions.
144 changes: 144 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# UIKeyboard

[![platform](https://img.shields.io/badge/Platform-iOS%208%2B-blue.svg)]()
[![language](https://img.shields.io/badge/Language-Swift-red.svg)]()
[![language](https://img.shields.io/badge/pod-4.0.0-blue.svg)]()
[![license](https://img.shields.io/badge/license-MIT-lightgray.svg)]()

- [Requirements](#requirements)

- [Installation](#installation)
- [CocoaPods](#CocoaPods)

- [Usage](#usage)

- [License](#license)

## Requirements

- iOS 8.0+
- Xcode 9.0+
- Swift 4.0+

## Installation
### Cocoa pods
To integrate `UIKeyboard` into your Xcode project using [CocoaPods](http://cocoapods.org), create `Podfile`. Run the following command in root folder of your project:

```bash
$ pod init
```
In the `Podfile` that appears, specify:

```ruby
platform :ios, ‘8.0

target '<Your Target Name>' do
use_frameworks!
pod 'UIKeyboard'
end
```

Then, run the following command:

```bash
$ pod install
```

## Usage
1. By default `UIViewController` have `UIKeyboard` property with `keyboard` name which notify about keyboard appearance changes

```swift
class UIKeyboardViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Default UIViewController keyboard property
self.keyboard

}

}
```

2. You can use `UIKeyboard` observers with closure or(and) delegate. Choose a method that is more convenient for you.

- with `UIKeyboardDelegate`:

```swift
extension UIKeyboardViewController: UIKeyboardDelegate {

func keyboardWillShow(with info: UIKeyboard.Info) {
// Posted immediately prior to the display of the keyboard
}

func keyboardDidShow(with info: UIKeyboard.Info) {
// Posted immediately after the display of the keyboard.
}

func keyboardWillHide(with info: UIKeyboard.Info) {
// Posted immediately prior to the dismissal of the keyboard.
}

func keyboardDidHide(with info: UIKeyboard.Info) {
// Posted immediately after the dismissal of the keyboard.
}

}
```

- with closures

```swift
class UIKeyboardViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

self.keyboard.willShow { (info) in
// Posted immediately prior to the display of the keyboard
}

self.keyboard.didShow { (info) in
// Posted immediately after the display of the keyboard.
}

self.keyboard.willHide { (info) in
// Posted immediately prior to the dismissal of the keyboard.
}

self.keyboard.didHide { (info) in
// Posted immediately after the dismissal of the keyboard.
}

}

}
```

3. `UIKeyboard.Info` have next properties:
```swift
/// Rect that identifies the ending frame rectangle of the keyboard in screen coordinates. The frame rectangle reflects the current orientation of the device.
public let frame: CGRect

/// Time interval that identifies the duration of the animation in seconds.
public let animationDuration: TimeInterval

/// Animation curve constant that defines how the keyboard will be animated onto or off the screen.
public let animationCurve: UIView.AnimationCurve

/// Animation options constant that defines how the keyboard will be animated onto or off the screen.
public var animationOptions: UIView.AnimationOptions
```

- Simple example of `info` usege for change `scrollView` content inset:
```swift
self.keyboard.willShow { (info) in
UIView.animate(withDuration: info.animationDuration, delay: 0, options: info.animationOptions, animations: {
self.scrollView.contentInset.bottom = info.frame.height
}, completion: nil)
}
```

## License
Released under the MIT license. See [LICENSE](https://github.com/YuriFox/UIKeyboard/blob/master/LICENSE) for details.
22 changes: 22 additions & 0 deletions Source/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
</dict>
</plist>
27 changes: 27 additions & 0 deletions Source/UIKeyboard+UIViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// UIKeyboard+UIViewController.swift
// UIKeyboard
//
// Created by Yuri Fox on 2/2/19.
// Copyright © 2019 Developer Lysytsia. All rights reserved.
//

import UIKit

fileprivate var keyboardKey = "UIViewControllerKeyboardKey"

extension UIViewController {

/// Default UIViewController keyboard property. Use for notify about keyboard appearence changes
public var keyboard: UIKeyboard {
if let k = objc_getAssociatedObject(self, &keyboardKey) as? UIKeyboard {
return k
}

let keyboard = UIKeyboard()
objc_setAssociatedObject(self, &keyboardKey, keyboard, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
return keyboard

}

}
19 changes: 19 additions & 0 deletions Source/UIKeyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// UIKeyboard.h
// UIKeyboard
//
// Created by Yuri Fox on 1/31/19.
// Copyright © 2019 Developer Lysytsia. All rights reserved.
//

//#import <UIKit/UIKit.h>
//
////! Project version number for UIKeyboard.
//FOUNDATION_EXPORT double UIKeyboardVersionNumber;
//
////! Project version string for UIKeyboard.
//FOUNDATION_EXPORT const unsigned char UIKeyboardVersionString[];
//
//// In this header, you should import all the public headers of your framework using statements like #import <UIKeyboard/PublicHeader.h>
//
//
112 changes: 112 additions & 0 deletions Source/UIKeyboard.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// UIKeyboard.swift
// UIKeyboard
//
// Created by Yuri Fox on 1/31/19.
// Copyright © 2019 Developer Lysytsia. All rights reserved.
//

import UIKit

public class UIKeyboard {

/// Keyboard delegates which notify about keyboard appearence changes
public weak var delegate: UIKeyboardDelegate?

private var willShowHandler: Handler?
private var didShowHandler: Handler?
private var willHideHandler: Handler?
private var didHideHandler: Handler?

public init() {
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: nil) { [weak self] (notification) in

guard let info = UIKeyboard.Info(notification: notification) else {
return
}

self?.willShowHandler?(info)
self?.delegate?.keyboardWillShow(with: info)

}

NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidShowNotification, object: nil, queue: nil) { [weak self] (notification) in

guard let info = UIKeyboard.Info(notification: notification) else {
return
}

self?.didShowHandler?(info)
self?.delegate?.keyboardDidShow(with: info)

}

NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) { [weak self] (notification) in

guard let info = UIKeyboard.Info(notification: notification) else {
return
}

self?.willHideHandler?(info)
self?.delegate?.keyboardWillHide(with: info)
}

NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidHideNotification, object: nil, queue: nil) { [weak self] (notification) in

guard let info = UIKeyboard.Info(notification: notification) else {
return
}

self?.didHideHandler?(info)
self?.delegate?.keyboardDidHide(with: info)
}

}

deinit {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidHideNotification, object: nil)
}

/// Posted immediately prior to the display of the keyboard
///
/// - Parameter info: The information about the keyboard.
@discardableResult
public func willShow(handler: @escaping Handler) -> UIKeyboard {
self.willShowHandler = handler
return self
}

/// Posted immediately after the display of the keyboard.
///
/// - Parameter info: The information about the keyboard.
@discardableResult
public func didShow(handler: @escaping Handler) -> UIKeyboard {
self.didShowHandler = handler
return self
}

/// osted immediately prior to the dismissal of the keyboard.
///
/// - Parameter info: The information about the keyboard.
@discardableResult
public func willHide(handler: @escaping Handler) -> UIKeyboard {
self.willHideHandler = handler
return self
}

/// Posted immediately after the dismissal of the keyboard.
///
/// - Parameter info: The information about the keyboard.
@discardableResult
public func didHide(handler: @escaping Handler) -> UIKeyboard {
self.didHideHandler = handler
return self
}

/// Closure with information about the keyboard.
public typealias Handler = (UIKeyboard.Info) -> Void

}
40 changes: 40 additions & 0 deletions Source/UIKeyboardDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// UIKeyboardDelegate.swift
// UIKeyboard
//
// Created by Yuri Fox on 2/2/19.
// Copyright © 2019 Developer Lysytsia. All rights reserved.
//

import Foundation

public protocol UIKeyboardDelegate: class {

/// Posted immediately prior to the display of the keyboard
///
/// - Parameter info: The information about the keyboard.
func keyboardWillShow(with info: UIKeyboard.Info)

/// Posted immediately after the display of the keyboard.
///
/// - Parameter info: The information about the keyboard.
func keyboardDidShow(with info: UIKeyboard.Info)

/// Posted immediately prior to the dismissal of the keyboard.
///
/// - Parameter info: The information about the keyboard.
func keyboardWillHide(with info: UIKeyboard.Info)

/// Posted immediately after the dismissal of the keyboard.
///
/// - Parameter info: The information about the keyboard.
func keyboardDidHide(with info: UIKeyboard.Info)

}

extension UIKeyboardDelegate {
func keyboardWillShow(with info: UIKeyboard.Info) { }
func keyboardDidShow(with info: UIKeyboard.Info) { }
func keyboardWillHide(with info: UIKeyboard.Info) { }
func keyboardDidHide(with info: UIKeyboard.Info) { }
}
Loading

0 comments on commit c553672

Please sign in to comment.