CommonKeyboard 1.0.8

CommonKeyboard 1.0.8

Kaweerut KanthawongKaweerut KanthawongKaweerut Kanthawong 维护。



  • Kaweerut Kanthawong

CommonKeyboard

一款优雅的 iOS 键盘库。简单、轻量级且独立,无需依赖任何子组件

Swift Swift

CommonKeyboard CommonKeyboardObserver

安装

CocoaPods

将以下内容添加到您的 Podfile

pod 'CommonKeyboard'

Carthage

将以下内容添加到您的 Cartfile

github "kaweerutk/CommonKeyboard"

使用方法

在 AppDelegate.swift 中,只需导入 CommonKeyboard 框架并启用 CommonKeyboard。

import CommonKeyboard

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // just enable this single line of code below
    // supported UIScrollView including inherited classes (e.g., UITableView or UICollectionView)
    //
    // *** This doesn't work with UITableViewController because they've built-in handler ***
    //
    CommonKeyboard.shared.enabled = true
    
    // uncomment this line to print the debug logs
    //CommonKeyboard.shared.debug = true
      
    return true
  }
}

当光标关注时,CommonKeyboard 会自动滚动到输入视图,并在空区域点击以关闭键盘。它与 UIScrollView 以及其所有子类(包括 UITableView 和 UICollectionView)一起工作(注意: 这不支持 UITableViewController,因为它会自行处理)。

通过设置 keyboardOffset 调整键盘和输入视图之间的偏移量,默认值为 10,或通过设置 ignoredCommonKeyboard 为真值忽略通用键盘。

 textField.keyboardOffset = 20
 textField.ignoredCommonKeyboard = true

 textView.keyboardOffset = 2
 textView.ignoredCommonKeyboard = false

CommonKeyboardObserver

您可以通过订阅 CommonKeyboardObserver 来获取键盘通知信息。

import CommonKeyboard

class ExampleChatViewController: UIViewController {
    @IBOutlet var tableView: UITableView!
    @IBOutlet var bottomConstraint: NSLayoutConstraint!
    let keyboardObserver = CommonKeyboardObserver()

    override func viewDidLoad() {
        super.viewDidLoad()
        // drag down to dismiss keyboard
        tableView.keyboardDismissMode = .interactive

        keyboardObserver.subscribe(events: [.willChangeFrame, .dragDown]) { [weak self] (info) in
            guard let self = self else { return }
            let bottom = info.isShowing
                ? (-info.visibleHeight) + self.view.safeAreaInsets.bottom
                : 0
            UIView.animate(info, animations: { [weak self] in
                self?.bottomConstraint.constant = bottom
                self?.view.layoutIfNeeded()
            })
        }
    }
}

所有事件

public enum CommonKeyboardObserverEvent {
    case willShow
    case didShow
    case willHide
    case didHide
    case willChangeFrame
    case didChangeFrame
    case dragDown // scroll.keyboardDismissMode = .interactive
}

在 UI 树视图中,有时有很多 UIScrollView 容器,并且 CommonKeyboard 无法找到正确的容器,您可以实现 CommonKeyboardContainerProtocol 并返回特定的容器。

extension ExampleChatViewController: CommonKeyboardContainerProtocol {
    var scrollViewContainer: UIScrollView {
        return tableView
    }
}

杂项

 // dismiss keyboard
 CommonKeyboard.shared.dismiss()

 // get current UIResponder
 let responder = CommonKeyboard.shared.currentResponder

调试

// enable debug mode to print out keyboard info
 CommonKeyboard.shared.debug = true


// ** Sample output **

/*
----- CommonKeyboard debug enabled -----
- isShowing:  true
- keyboardFrameBegin:  (0.0, 896.0, 414.0, 243.0)
- keyboardFrameEnd:  (0.0, 550.0, 414.0, 346.0)
- visibleHeight:  346.0
- isLocal:  true
- scrollContainer:  <UITableView: 0x103820e00; frame = (0 92; 414 700); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x28223a310>; layer = <CALayer: 0x282cf2960>; contentOffset: {0, 0}; contentSize: {414, 0}; adjustedContentInset: {0, 0, 0, 0}; dataSource: (null)>
------
*/

// ** Sample output when CommonKeyboard could not find `scrollContainer` **

/*
 ----- CommonKeyboard debug enabled -----
- isShowing:  true
- keyboardFrameBegin:  (0.0, 896.0, 414.0, 243.0)
- keyboardFrameEnd:  (0.0, 550.0, 414.0, 346.0)
- visibleHeight:  346.0
- isLocal:  true
- scrollContainer:    
   ***** 
     COULD NOT FIND `scrollContainer` 
     YOU BETTER TO IMPLEMENT `CommonKeyboardContainerProtocol` 
     IN `topViewController` (<KeyboardExample.FormViewController: 0x10570a150> 
     TO RETURN SPECIFIC `scrollContainer` 
   *****
------
*/

需求

  • iOS12 或更高版本
  • Swift 5.0 或更高版本

联系方式

如果您有任何问题或问题,请创建一个 问题

许可证

CocoaKeyboard许可证为 MIT 许可证。