Last active
October 19, 2017 16:39
-
-
Save LeoValentim/a21b7a8c33e34c9053da72dcc8849448 to your computer and use it in GitHub Desktop.
KeyboardProtocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// KeyboardProtocol.swift | |
// | |
// Created by Mac on 21/06/17. | |
// Copyright © 2017 Leo Valentim. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
@objc public protocol KeyboardProtocol : NSObjectProtocol { | |
@objc optional func hideKeyboardWhenTappedAround() | |
@objc optional func registerKeyboardInView() | |
func keyboardWillShow(_ notification: Notification) | |
func keyboardWillHide(_ notification: Notification) | |
} | |
extension KeyboardProtocol { | |
func hideKeyboardWhenTappedAround(view: UIView, action: Selector) { | |
//Looks for single or multiple taps. | |
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: action) | |
view.addGestureRecognizer(tap) | |
} | |
func registerKeyboardInView(_ viewController: UIViewController) { | |
NotificationCenter.default.addObserver(viewController, selector: #selector(keyboardWillShow(_:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil) | |
NotificationCenter.default.addObserver(viewController, selector: #selector(keyboardWillHide(_:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil) | |
} | |
} | |
/* | |
var keyboardSize : CGRect! = nil | |
var keyboardOpen : Bool = false | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// ======= KeyboardProtocol ======= // | |
self.hideKeyboardWhenTappedAround(view: self.view, action: #selector(self.dismissKeyboard)) | |
self.registerKeyboardInView(self) | |
} | |
func dismissKeyboard() { | |
self.view.endEditing(true) | |
} | |
// ======= KeyboardProtocol ======= // | |
func keyboardWillShow(_ notification: Notification) { | |
if (self.keyboardSize) == nil { | |
self.keyboardSize = ((notification as NSNotification).userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue | |
if self.keyboardSize.height == 0 { | |
self.keyboardSize.size.height = 250.0 | |
} | |
} | |
if !keyboardOpen { | |
self.scrollView.contentSize.height = self.scrollView.contentSize.height + self.keyboardSize.height | |
} | |
self.keyboardOpen = true | |
} | |
func keyboardWillHide(_ notification: Notification) { | |
if (self.keyboardSize) == nil { | |
self.keyboardSize = ((notification as NSNotification).userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue | |
if self.keyboardSize.height == 0 { | |
self.keyboardSize.size.height = 250.0 | |
} | |
} | |
if keyboardOpen { | |
self.scrollView.contentSize.height = self.scrollView.contentSize.height - self.keyboardSize.height | |
} | |
self.keyboardOpen = false | |
} | |
// ======= KeyboardProtocol ======= // | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment