Skip to content

Commit

Permalink
No need to unregister
Browse files Browse the repository at this point in the history
  • Loading branch information
chanonly123 committed Jun 23, 2022
1 parent 154d8d2 commit 380f2b7
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ xcuserdata/
!*.xcworkspace/contents.xcworkspacedata
/*.gcno

# End of https://www.gitignore.io/api/xcode
# End of https://www.gitignore.io/api/xcode
*.DS_Store
11 changes: 2 additions & 9 deletions Auto Keyborad Demo/Auto Keyborad Demo/ScrollViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ class ScrollViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
}

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

registerAutoKeyboard { result in
print("ScrollViewController: keyboard status \(result.status)")
// to keep scroll position, also works with tableView
// please restrict rotation, otherwise wont work correctly
if result.status == .willChangeFrame && self.scrollView.reachedBottom {
Expand All @@ -32,11 +30,6 @@ class ScrollViewController: UIViewController {
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
unRegisterAutoKeyboard()
}

@IBAction func bResingTap(_ sender: Any) {
tfAny.resignFirstResponder()
}
Expand Down
10 changes: 1 addition & 9 deletions Auto Keyborad Demo/Auto Keyborad Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,12 @@ class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
}

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

registerAutoKeyboard(enable: [lblBottom], disable: [btnShowScrollBottom]) { result in
print("keyboard status \(result.status)")
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
unRegisterAutoKeyboard()
}

@IBAction func bResingTap(_ sender: Any) {
tfAny.resignFirstResponder()
}
Expand Down
4 changes: 2 additions & 2 deletions AutoKeyboard.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "AutoKeyboard"
s.version = "1.4.7"
s.version = "1.4.8"
s.summary = "Automatic Keyboard handling with ease"
s.description = "Automatic bottom constraints changes with extreamly easy integration."
s.homepage = "https://github.com/chanonly123/AutoKeyboard"
Expand Down Expand Up @@ -80,7 +80,7 @@ Pod::Spec.new do |s|
# Supports git, hg, bzr, svn and HTTP.
#

s.source = { :git => "https://github.com/chanonly123/AutoKeyboard.git", :tag => "1.4.7" }
s.source = { :git => "https://github.com/chanonly123/AutoKeyboard.git", :tag => "1.4.8" }


# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
4 changes: 2 additions & 2 deletions AutoKeyboard/AutoKeyboard.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.4.7;
MARKETING_VERSION = 1.4.8;
PRODUCT_BUNDLE_IDENTIFIER = com.chanonly123.AutoKeyboard;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand All @@ -282,7 +282,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.4.7;
MARKETING_VERSION = 1.4.8;
PRODUCT_BUNDLE_IDENTIFIER = com.chanonly123.AutoKeyboard;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand Down
21 changes: 20 additions & 1 deletion AutoKeyboard/AutoKeyboard/AutoKeyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ extension UIViewController {
}

/**
Unregisters keyboard observers.
Unregisters keyboard observers. Can be called from deinit (optional).
*/
public func unRegisterAutoKeyboard() {
print("Unregistering AutoKeyboard from: \(NSStringFromClass(type(of: self)))")
Expand All @@ -104,6 +104,7 @@ extension UIViewController {
}

@objc func actionKeyboardWillShow(notification: NSNotification) {
if view.firstResponder == nil { return }
if let result = decodeNotification(notification: notification, status: .willShow) {
if let group = savedObservers.object(forKey: self) {
let saved = group.constraints
Expand All @@ -125,6 +126,7 @@ extension UIViewController {
}

@objc func actionKeyboardWillHide(notification: NSNotification) {
if view.firstResponder == nil { return }
if let result = decodeNotification(notification: notification, status: .willHide) {
if let group = savedObservers.object(forKey: self) {
let saved = group.constraints
Expand All @@ -138,24 +140,28 @@ extension UIViewController {
}

@objc func actionKeyboardDidShow(notification: NSNotification) {
if view.firstResponder == nil { return }
if let result = decodeNotification(notification: notification, status: .didShow) {
savedObservers.object(forKey: self)?.handler?(result)
}
}

@objc func actionKeyboardDidHide(notification: NSNotification) {
if view.firstResponder == nil { return }
if let result = decodeNotification(notification: notification, status: .didHide) {
savedObservers.object(forKey: self)?.handler?(result)
}
}

@objc func actionKeyboardWillChangeFrame(notification: NSNotification) {
if view.firstResponder == nil { return }
if let result = decodeNotification(notification: notification, status: .willChangeFrame) {
savedObservers.object(forKey: self)?.handler?(result)
}
}

@objc func actionKeyboardDidChangeFrame(notification: NSNotification) {
if view.firstResponder == nil { return }
if let result = decodeNotification(notification: notification, status: .didChangeFrame) {
savedObservers.object(forKey: self)?.handler?(result)
}
Expand Down Expand Up @@ -229,5 +235,18 @@ extension UIViewController {
self!.view.layoutIfNeeded()
}, completion: nil)
}

}

extension UIView {
var firstResponder: UIView? {
guard !isFirstResponder else { return self }
for subview in subviews {
if let firstResponder = subview.firstResponder {
return firstResponder
}
}
return nil
}
}

10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Automatic Keyboard handling with ease. It is fully automatic keyboard handling.
- Automatic bottom constraints changes with keyboard
- Resizing with animation.
- No need to write extra code.
- Just `registerAutoKeyboard ` and `unRegisterAutoKeyboard `.
- Just `registerAutoKeyboard ` in `viewDidLoad`.
- Callback support on keyboard willShow, didShow, willHide, didHide, willChangeFrame, didChangeFrame.
- Example for keeping scroll position of scrollView
- Ability to Register or Disable other constraints
Expand All @@ -31,12 +31,8 @@ Automatic Keyboard handling with ease. It is fully automatic keyboard handling.
- Add constrainsts to `bottomLayoutGuide` or `safeAreaLayoutGuide` and they will update when keyboard appears.
- And Register your specific ViewController, you should also unregister.
```Swift
override func viewWillAppear(_ animated: Bool) {
registerAutoKeyboard()
}

override func viewWillDisappear(_ animated: Bool) {
unRegisterAutoKeyboard()
override func viewDidLoad() {
registerAutoKeyboard()
}
```
### Advanced Usage
Expand Down

0 comments on commit 380f2b7

Please sign in to comment.