在 swift4 中点击文本字段时无法查看
Unable to make View up when tapping on textfield in swift4
我在 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()
})
}
}
}
你可以试试正面的keyboardFrame!.height
viewbottomConstraint?.constant = isKeyboardShowing ? keyboardFrame!.height : 0
我在 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()
})
}
}
}
你可以试试正面的keyboardFrame!.height
viewbottomConstraint?.constant = isKeyboardShowing ? keyboardFrame!.height : 0