简体   繁体   English

swift4中移动视图时屏幕为黑色

[英]screen is black when move view in swift4

I have general message view controller I am sending message using textfield but keyboard is covered the text fields. 我有通用的消息视图控制器,我正在使用文本字段发送消息,但是键盘覆盖了文本字段。 I can refer Move view with keyboard using Swift this link added some code but after that some portion shows black. 我可以使用Swift使用键盘引用“ 移动”视图,此链接添加了一些代码,但之后部分显示为黑色。 how to avoid the screen black after sending the message 发送消息后如何避免屏幕变黑

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

this is code 这是代码

   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
           }
        }
   }

Your code is just wrong. 您的代码是错误的。 You have no business changing the frame of self.view . 您无需更改self.viewframe The main view of a view controller must be left in place where it is. 视图控制器的主视图必须保留在原处。 Wrap your interface in a subview of the main view and move the subview (if you insist on using this approach to avoiding being covered by the keyboard). 将界面包装在主视图的子视图中,然后移动子视图(如果您坚持使用这种方法以避免被键盘遮盖)。

Try changing your end y origin back to 0 instead of a calculated value. 尝试将y端的原点更改回0而不是计算值。

   @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 == 0
           }
        }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM