我有一个自定义UIViewController,在其中实现了一个自定义用户输入对话框。当用户点击UITextField时,出现键盘,并且消息窗口向上移动。
该代码首次正常运行。但是,当我关闭包含自定义消息对话框的视图控制器,并再次对其进行设置时,实现用户输入对话框的UIView似乎无法正确移动(缺少我输入的偏移量)
首先,我实现了对UIViewController的扩展(通过使用this post和this one中的代码示例)
extension UIViewController {
// forHeight is the height of the message view that will be shifted up/down
func setNotificationCentreForKeyboardEvents(forHeight:CGFloat) {
// Set the selector functions for NSNotification.Name.UIKeyboardWillShow/Hide
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: Notification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: Notification.Name.UIKeyboardWillHide, object: nil)
}
// In order for this function to work, the "tag" the message view that is goingto be moved up
// needs to be set to 55
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue,
let messageView = self.view.viewWithTag(55) {
// create a rect with the dimensions of the keyboard
let aRectY:CGFloat = self.view.frame.height - keyboardSize.height
let aRect = CGRect(x: 0, y: aRectY, width: self.view.frame.size.width, height: keyboardSize.height)
// find the inersection of this rect with the message view
let intersection = aRect.intersection(messageView.frame)
if self.view.frame.origin.y == 0 {
self.view.frame.origin.y -= intersection.size.height+20
}
}
}
// In order for this function to work, the "tag" the message view that is goingto be moved down
// needs to be set to 55
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue,
let messageView = self.view.viewWithTag(55) {
// create a rect with the dimensions of the keyboard
let aRectY:CGFloat = self.view.frame.height - keyboardSize.height
let aRect = CGRect(x: 0, y: aRectY, width: self.view.frame.size.width, height: keyboardSize.height)
// find the inersection of this rect with the message view
let intersection = aRect.intersection(messageView.frame)
if self.view.frame.origin.y == 0 {
self.view.frame.origin.y += intersection.size.height - 20
}
}
}
}
在包含用户输入对话框的自定义UIViewController中,我有
override func viewDidLoad() {
...
messageView.tag = 55
setNotificationCentreForKeyboardEvents(forHeight: messageView.frame.size.height)
...
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
并且,对包含自定义用户输入对话框的视图执行segue的viewController具有
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == showItemSegueName) {
let destination = segue.destination as! ItemsViewController
destination.listName = selectedListName
} else if (segue.identifier == "addListSegue") {
let newController: AddListingDialogViewController = segue.destination as! AddListingDialogViewController
newController.modalPresentationStyle = .overFullScreen
}
}
这是它的行为
我在这里想念什么?为什么在第一次触发segue后代码无法按预期工作?
谢谢