From eab630a4ee495a2b3bcf491dc10e369924c15c73 Mon Sep 17 00:00:00 2001 From: stefan draskovits Date: Tue, 22 Jun 2021 16:23:39 +0200 Subject: [PATCH] change modifier so subclasses can override functions --- Sources/Toolbox/Utilities/Keyboard.swift | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Sources/Toolbox/Utilities/Keyboard.swift b/Sources/Toolbox/Utilities/Keyboard.swift index 9215e36..a0b2813 100644 --- a/Sources/Toolbox/Utilities/Keyboard.swift +++ b/Sources/Toolbox/Utilities/Keyboard.swift @@ -6,11 +6,19 @@ import UIKit @available(iOS 13.0, macOS 10.15, *) open class Keyboard { public struct Info { + public let keyboardBeginFrame: CGRect public let keyboardEndFrame: CGRect public let animationDuration: TimeInterval public let animationOptions: UIView.AnimationOptions + public init(keyboardBeginFrame: CGRect, keyboardEndFrame: CGRect, animationDuration: TimeInterval, animationOptions: UIView.AnimationOptions) { + self.keyboardBeginFrame = keyboardBeginFrame + self.keyboardEndFrame = keyboardEndFrame + self.animationDuration = animationDuration + self.animationOptions = animationOptions + } + public var keyboardHeight: CGFloat { return keyboardEndFrame.height } @@ -18,7 +26,7 @@ open class Keyboard { public static let shared = Keyboard() - private(set) var isShown: Bool = false + public private(set) var isShown: Bool = false private var cancellables = Set() @Published public var info = Info(keyboardBeginFrame: .zero, keyboardEndFrame: .zero, animationDuration: 0.0, animationOptions: []) @@ -70,8 +78,9 @@ open class Keyboard { // MARK: - UIViewController @available(iOS 13.0, macOS 10.15, *) -public extension UIViewController { - func updateSafeAreaInsets(keyboardInfo: Keyboard.Info, keyboardFrame: CGRect? = nil, animated: Bool) { +extension UIViewController { + + open func updateSafeAreaInsets(keyboardInfo: Keyboard.Info, keyboardFrame: CGRect? = nil, animated: Bool) { let keyboardFrameInView = view.convert(keyboardFrame ?? keyboardInfo.keyboardEndFrame, from: nil) let safeAreaFrame = view.safeAreaLayoutGuide.layoutFrame.insetBy(dx: 0, dy: -additionalSafeAreaInsets.bottom) let intersection = safeAreaFrame.intersection(keyboardFrameInView) @@ -95,13 +104,13 @@ public extension UIViewController { } } - func hideKeyboardOnTap() { + open func hideKeyboardOnTap() { let recognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)) recognizer.cancelsTouchesInView = false view.addGestureRecognizer(recognizer) } - @objc func dismissKeyboard() { + @objc open func dismissKeyboard() { // Needs to be executed in the next run loop so that buttons have enough time to register being touched // before the keyboard disappears. DispatchQueue.main.async { [weak self] in