ButtonOnKeyboard 0.2.0

ButtonOnKeyboard 0.2.0

TWSlidingView维护。



  • 作者:
  • magicmon

ButtonOnKeyboard

CI Status Version License Platform Language

ButtonOnKeyboard 适用于必须根据键盘调整按钮位置的场合。它提供基本和刘海屏设计两种样式,同时也支持 UIButton 和 UIView。

入门

在 viewDidLoad() 中存储按钮的默认大小。

button.bk_defaultButtonHeight = 50  // example size.

每当键盘状态改变时,它会调用 button.bk_onKeyboard()。

button.bk_onKeyboard(keyboardHeight: height)

如果 UITextField 不通过 UIButton 显示,则将.scrollView 参数添加到 "bk_onKeyboard()" 中。

button.bk_onKeyboard(scrollView: scrollView, keyboardHeight: height)

Demo Demo2

示例

override func viewDidLoad() {
    super.viewDidLoad()
    
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    
    button.bk_defaultButtonHeight = buttonHeightConstraint.constant  // Stores the default size of the button.
}

@objc func keyboardWillShow(_ notification: Notification) {
    var visibleHeight: CGFloat = 0
     
    if let userInfo = notification.userInfo {
        if let windowFrame = UIApplication.shared.keyWindow?.frame,
            let keyboardRect = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            visibleHeight = windowFrame.intersection(keyboardRect).height
        }
    }
    
    updateButtonLayout(height: visibleHeight)
}
 
@objc func keyboardWillHide(_ notification: Notification) {
    updateButtonLayout(height: 0)
}

func updateButtonLayout(height: CGFloat) {
    button.bk_onKeyboard(scrollView: scrollView, keyboardHeight: height)
}

要求

  • Swift 5.0
  • iOS 11.0+

示例

要运行示例项目,首先从仓库克隆,然后在 Example 目录中运行 pod install

需求

安装

ButtonOnKeyboard 通过 CocoaPods 可用。要安装它,只需将以下行添加到您的 Podfile 中

pod 'ButtonOnKeyboard'

作者

magicmon, [email protected]

许可证

ButtonOnKeyboard 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。