编辑Swift 4时TextField消失

时间:2018-11-19 21:58:15

标签: ios swift uitextfield

所以我有一个ViewController,在它里面有一个TableView,在它的底部是一个包含TextField和Button的View。

当键盘出现时,我试图将View放在键盘上方的底部,并且仅当我将View放在前面时,我的View才会出现: self.view.bringSubviewToFront(SendMessageView) (为清楚起见,我的视图是可见的,我可以看到它,只有当我尝试将其出现在键盘上方时,它才会消失) 现在,当我在键盘上方移动视图时,可以立即看到视图,而当我开始键入某些内容时,视图便会消失,而且我不知道为什么,也不知道如何解决此问题。 我希望我能清楚地解释我的问题 在此之下,您将找到我的完整代码:

import UIKit
import Foundation

class Chat_user: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

@IBOutlet weak var SendMessageView: UIView!
@IBOutlet weak var SendMessageInput: UITextField!
@IBOutlet weak var SendButton: UIButton!
@IBOutlet weak var MessageList: UITableView!

var cellMessage: CustomMessage!
var count = 0

override func viewDidLoad() {
    super.viewDidLoad()

    let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.test))
    singleTapGestureRecognizer.numberOfTapsRequired = 1
    singleTapGestureRecognizer.isEnabled = true
    singleTapGestureRecognizer.cancelsTouchesInView = false
    self.view.addGestureRecognizer(singleTapGestureRecognizer)
    SendMessageInput.delegate = self
    self.view.bringSubviewToFront(SendMessageView)
    self.view.bringSubviewToFront(SendMessageInput)
    self.view.bringSubviewToFront(SendButton)
    self.view.sendSubviewToBack(MessageList)
    self.tabBarController?.tabBar.isHidden = true
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}

@objc func test(sender: UITapGestureRecognizer) {
    self.view.endEditing(true)
}

override func viewWillDisappear(_ animated: Bool) {
    self.tabBarController?.tabBar.isHidden = false
}

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        self.SendMessageView.frame.origin.y -= keyboardSize.height
        self.MessageList.frame.origin.y -= keyboardSize.height
        let indexPath = NSIndexPath(row: self.count - 1, section: 0)
        self.MessageList.scrollToRow(at: indexPath as IndexPath, at: .bottom, animated: true)
    }
}

@objc func keyboardWillHide(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        self.SendMessageView.frame.origin.y += keyboardSize.height
        self.MessageList.frame.origin.y += keyboardSize.height
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    self.count = 10
    return (self.count)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    self.cellMessage = tableView.dequeueReusableCell(withIdentifier: "message", for: indexPath) as? CustomMessage
    return (cellMessage)
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    self.view.endEditing(true)
    return true
}
}

预先感谢

询问

1 个答案:

答案 0 :(得分:1)

这是帧布局的一个已知问题,您需要将SendMessageView的底部约束拖动到内部

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        self.SendMessageViewBottCon.constant += keyboardSize.height
        self.view.layoutIfNeeded()
        let indexPath = NSIndexPath(row: self.count - 1, section: 0)
        self.MessageList.scrollToRow(at: indexPath as IndexPath, at: .bottom, animated: true)
    }
}

@objc func keyboardWillHide(notification: NSNotification) { 
      self.SendMessageViewBottCon.constant = 0 
      self.view.layoutIfNeeded() 
}

如果您需要动画插入

UIView.animate(withDuration: 0.25, animations: {
     self.view.layoutIfNeeded()
})