簡體   English   中英

swift4中移動視圖時屏幕為黑色

[英]screen is black when move view in swift4

我有通用的消息視圖控制器,我正在使用文本字段發送消息,但是鍵盤覆蓋了文本字段。 我可以使用Swift使用鍵盤引用“ 移動”視圖,此鏈接添加了一些代碼,但之后部分顯示為黑色。 發送消息后如何避免屏幕變黑

在此處輸入圖片說明

在此處輸入圖片說明

在此處輸入圖片說明

這是代碼

   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 視圖控制器的主視圖必須保留在原處。 將界面包裝在主視圖的子視圖中,然后移動子視圖(如果您堅持使用這種方法以避免被鍵盤遮蓋)。

嘗試將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