【问题标题】:Swift - How to make the UITextField visible when software keyboard is being used by it? [duplicate]Swift - 如何在使用软件键盘时使 UITextField 可见? [复制]
【发布时间】:2018-08-02 11:29:02
【问题描述】:
【问题讨论】:
标签:
swift
uitextfield
focus
uikeyboard
uitextfielddelegate
【解决方案1】:
您可以使用以下库在键盘出现时自动调整屏幕高度。
-
IQ Key Borad 实现:- 在 Appdelegate
didFinishLaunchingWithOptions 中添加了以下代码
IQKeyboardManager.shared.enable = true
注意:- 如果您使用 IQ 键盘工具栏将始终出现在键盘上方。
-
TP KeyBoard (要实现这一点,您应该将 scroll/TableView/CollectionView 类设置为 TPkeyboardAvoidingScrollView/TPkeyboardAvoidingTableView/TPkeyboardAvoidingCollectionView)
或
你应该使用键盘通知来调整高度约束。
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
//MARK:- 键盘通知
@objc func keyboardWillShow(_ notification:Notification) {
if let height = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
selectedImageViewToBottom.constant = height
}
}
@objc func keyboardWillHide(_ notification:Notification) {
selectedImageViewToBottom.constant = 0
}