gpt4 book ai didi

ios - 在 swift4 中移动 View 时屏幕是黑色的

转载 作者:行者123 更新时间:2023-11-28 14:50:11 25 4
gpt4 key购买 nike

我有通用消息 View Controller ,我正在使用文本字段发送消息,但键盘覆盖了文本字段。我可以引用 Move view with keyboard using Swift 此链接添加了一些代码,但之后某些部分显示为黑色。如何避免发送消息后黑屏

enter image description here

enter image description here

enter image description here

这是代码

   override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(GenralMessageViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(GenralMessageViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}


@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0 {
self.view.frame.origin.y -= keyboardSize.height
}
}
}

@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
}
}
}

最佳答案

你的代码是错误的。您无权更改 self.viewframe。 View Controller 的主视图必须留在原处。将您的界面包裹在主视图的 subview 中并移动 subview (如果您坚持使用这种方法以避免被键盘覆盖)。

关于ios - 在 swift4 中移动 View 时屏幕是黑色的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49926691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com