swift - 键盘上方的视图不显示
问题描述
当显示键盘时,我希望在我的键盘上方有一个带有 textField 的视图和一个发送按钮。但这不起作用。
我已经在我的代码中实现了这一点:
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_ :)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tabBarController?.tabBar.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
tabBarController?.tabBar.isHidden = false
}
// MARK: - Keyboard stuff
@objc func keyboardWillShow(_ notification: NSNotification) {
let keyboardFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
print("hier ist \(keyboardFrame!)")
UIView.animate(withDuration: 0.1) {
self.bottomConstraint.constant = keyboardFrame!.height
self.view.layoutIfNeeded()
}
}
@objc func keyboardWillHide(_ notification: NSNotification) {
UIView.animate(withDuration: 0.1) {
self.bottomConstraint.constant = 0
self.view.layoutIfNeeded()
}
}
解决方案
查看 textField inputAccessoryView。这里有一个教程解释了你想要做什么。
推荐阅读
- azure - ansible Zypper 模块中的 with_items 未调用库存文件变量
- java - URL 图片不使用增强现实 (AR) Android 播放视频
- javascript - 填写所有字段后禁用提交按钮(纯javascript)
- asciidoc - asciidoc 在句号“:”后删除缩进
- php - 如何在数据库查询中使用会话和数组来存储和显示数据
- python - 如何从所有可能的组合中获得所有可能的独特变体
- firebase - Firebase - 在 Cordova 中从 Direct Channel API 切换到 FCM HTTP v1 API
- python-3.x - Python Libcst:无法从访问者类中的节点生成代码
- android - “compileSdkVersion 未指定。请将其添加到 build.gradle ”,即使添加了 compileSdkVersion
- scala - Spark Scala数据框中的列拆分