ios - 单击tableView中的文本字段时,ScrollView没有向上移动
问题描述
我试过这段代码:在 viewWillAppear 中:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: Notification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
和 :
@objc func keyboardWillAppear(notification:NSNotification) {
print("keyboard appear")
var info = notification.userInfo
let keyBoardSize = info![UIKeyboardFrameEndUserInfoKey] as! CGRect
scrollCreateEditContact.contentInset = UIEdgeInsetsMake(0.0, 0.0, keyBoardSize.height, 0.0)
scrollCreateEditContact.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, keyBoardSize.height, 0.0)
}
@objc func keyboardWillDisappear(notification:NSNotification) {
print("keyboard disappear")
scrollCreateEditContact.contentInset = UIEdgeInsets.zero
scrollCreateEditContact.scrollIndicatorInsets = UIEdgeInsets.zero
}
我想要的是当键盘出现时文本字段没有被键盘覆盖:
该代码仅适用于不在 tableView 内的文本字段。但是,当我单击 tableView 内的文本字段并记录它时,总是检测到“键盘出现”。
当键盘出现时,tableView 中的文本字段的正确代码是什么?
解决方案
这是常见的行为,与 android 不同,ios 无法自动调整键盘上方的内容。我的解决方案是,您可以将所有视图(照片、文本字段等)包装在 tableView 中。并使用TPKeyboardAvoiding
图书馆。
pod 'TPKeyboardAvoiding'
如果您使用情节提要,请将 tableView 基类设置为TPKeyboardAvoidingTableView
.
推荐阅读
- python-3.x - 字体更改菜单按钮不会在 tkinter 文本编辑器中切换字体
- 7zip - 如何在不创建子文件夹的情况下将多个文件夹压缩到压缩文件夹中?
- mongodb - 使用 gqlgen 从 mongoDB 获取 _id
- c# - 有没有办法将 ContentControl 转换为此
- python-3.x - 在python中使用scrapy来抓取需要向下滚动的数据
- angular - Angular ngFor 不适用于我的对象
- android-studio - 是否可以以编程方式重新着色乐透动画?
- python - python:关于 lambda 和闭包
- mongoose - 在 NestJS 中使用 Mongoose 鉴别器时出现 OverwriteModelError
- scala - 如何找到网格邻居(x,y 作为整数)将它们分组并计算它们在 spark 中的值的平均值