问题描述
我知道有关此主题的一些问题已发布,但没有一个起作用.我有一个tableview,在它下面有一个UIView,其中包含一个文本字段和按钮.当我的键盘出现时,它覆盖了整个UIView,使我无法单击发送".显示我的应用程序外观的图像:
I know there are a few posted questions on this topic, but none of them worked. I have a tableview and under it there's a UIView that contains a textfield and button. When my keyboard appears, it covers the whole UIView, making me unable to click "Send". The image to show how my app looks like :
此刻,我的代码是:
override func viewDidLoad() {
super.viewDidLoad()
inputTextField.delegate = self
inputTextField.layer.cornerRadius = 5
inputTextField.clipsToBounds = true
sendButton.layer.cornerRadius = 5
sendButton.clipsToBounds = true
self.tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "TableViewCell")
loadMsg()
self.hideKeyboardWhenTappedAround()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
inputTextField.becomeFirstResponder()
}
当键盘出现时,应该如何将UIView连同UIView包含的文本字段和按钮一起向上移动?
How should I move the UIView up along with the textfield and button that is contained by the UIView when my keyboard appears?
推荐答案
这里是解决方案,创建发送按钮视图的底部布局约束参考
Here is solution, Create bottom layout constraint reference of send button view
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
@IBOutlet weak var sendbuttonView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self)
}
@objc func handleKeyboardNotification(_ notification: Notification) {
if let userInfo = notification.userInfo {
let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
let isKeyboardShowing = notification.name == NSNotification.Name.UIKeyboardWillShow
bottomConstraint?.constant = isKeyboardShowing ? -keyboardFrame!.height : 0
UIView.animate(withDuration: 0.5, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}
}
这篇关于当键盘出现时将文本框和按钮向上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!