【发布时间】:2021-06-03 03:21:28
【问题描述】:
我在 containerview 中有文本字段和按钮,现在如果我点击文本字段,我需要 containerview 必须使用键盘
底视图
bottom = leading = trailing = 0, height = 80
和
i have created containerview bottom constraint to NSLayoutConstraint
并添加了这样的代码:但我无法移动容器视图,只有键盘来了..视图不来了,我哪里错了
class MessageDetailsVCViewController: UIViewController {
@IBOutlet weak var messageTextfield: UITextField!
@IBOutlet weak var viewbottomConstraint: NSLayoutConstraint!
@IBOutlet weak var bottomContainerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: UIResponder.keyboardWillHideNotification, object: nil)
// Do any additional setup after loading the view.
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.bottomContainerView.superview?.setNeedsLayout()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func resignFirstResponder() -> Bool {
return true
}
@objc func handleKeyboardNotification(_ notification: Notification) {
if let userInfo = notification.userInfo {
let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
let isKeyboardShowing = notification.name == UIResponder.keyboardWillShowNotification
viewbottomConstraint?.constant = isKeyboardShowing ? -keyboardFrame!.height : 0
UIView.animate(withDuration: 0.5, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}
}
}
【问题讨论】:
-
@Pancho,同样,视图不会出现