"ButtonOnKeyboard" is used when the button position must change depending on the keyboard. Available in both basic and notch designs. It is also available for UIButton, as well as UIView.
In viewDidLoad(), stores the default size of the button.
button.bk_defaultButtonHeight = 50 // example size.
Whenever the keyboard state changes, it calls button.bk_onKeyboard(). If UITextField is not visible by UIButton, add the scrollView parameter to "bk_onKeyboard()".
button.bk_onKeyboard(keyboardHeight: height)
or
button.bk_onKeyboard(scrollView: scrollView, keyboardHeight: height)
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+
To run the example project, clone the repo, and run pod install
from the Example directory first.
ButtonOnKeyboard is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'ButtonOnKeyboard'
magicmon, sagun25si@gmail.com
ButtonOnKeyboard is available under the MIT license. See the LICENSE file for more info.