[英]Unable to make View up when tapping on textfield in swift4
I have textfield and button inside containerview, now if i tap on textfield i need containerview has to up with keyboard我在容器视图中有文本字段和按钮,现在如果我点击文本字段,我需要容器视图必须使用键盘
according to this answer根据这个答案
for bottom view用于底视图
bottom = leading = trailing = 0, height = 80
and和
i have created containerview bottom constraint to NSLayoutConstraint
and addd code like this: but i am unable to move container viewup, only keyboard coming.. view not comingup, where am i wrong并添加了这样的代码:但我无法移动容器视图,只有键盘来了..视图没有来,我错在哪里
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()
})
}
}
}
You can try positive keyboardFrame..height
你可以试试正面的
keyboardFrame..height
viewbottomConstraint?.constant = isKeyboardShowing ? keyboardFrame!.height : 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.