点击发送按钮时如何防止键盘隐藏在UITextView中?

时间:2018-12-02 02:44:13

标签: ios swift uitextview

我有UITextView,在它旁边并且与文本视图分开是一个发送按钮。用户在文本视图中键入文本并点击发送按钮后,键盘将隐藏。用户单击发送按钮后,我不想隐藏键盘。我该如何实现?

更新问题以提供更多详细信息。 1)发送按钮是一个单独的UIButton。 2)我没有在代码中专门调用任何resignFirstResponder。 3)单击“发送”按钮后,我将遵循显示/隐藏键盘的代码。

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

// Setup keyboard event
@objc func keyboardWillShow(notification:NSNotification){
    print("keyboardWillShow")
    var userInfo = notification.userInfo!
    var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    keyboardFrame = self.view.convert(keyboardFrame, from: nil)

    var contentInset:UIEdgeInsets = self.scrollView.contentInset
    contentInset.bottom = keyboardFrame.size.height
    scrollView.contentInset = contentInset
}

@objc func keyboardWillHide(notification:NSNotification){
    print("keyboardWillHide")
    let contentInset:UIEdgeInsets = UIEdgeInsets.zero
    scrollView.contentInset = contentInset
}

发送按钮点击的代码

@IBAction func sendButtonClicked(_ sender: UIButton) {

    // Disable textview and send button while message is being sent
    messageTextView.isEditable = false
    sendButton.isEnabled = false

    fetchSendMessage( ) { (result, error) in
        if error != nil {
            ShowDialog.showDialog(title: nil, message: (error?.localizedDescription)!, viewController: self)
        } else {
            if (result?.success)! {
                self.arry = (result?.data)!

                self.fetchMessages(page: 1, completed: {
                    self.insertNewMessage(sentMessage)
                })

                // Clear the message & Enable textview and send button
                self.messageTextView.isEditable = true
                self.messageTextView.text = "Type Message"
                self.messageTextView.textColor = UIColor.greyColour
                self.sendButton.isEnabled = true
                self.messageTextView.endEditing(true)
            } else {
                ShowDialog.showDialog(title: "Message not sent", message: (result?.errors![0].message)!, viewController: self)

                // Retain typed message
                // Enable textview and send button
                self.messageTextView.isEditable = true
                self.sendButton.isEnabled = true
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您可以在UITextViewDelegate中覆盖方法

func textViewShouldEndEditing(UITextView) -> Bool

让此方法返回false且UITextView不应辞职第一响应者,即关闭键盘。

答案 1 :(得分:0)

当我们尝试禁用UITextView's可编辑选项或UserInteraction时,键盘会自动隐藏。

代替这样做,

messageTextView.isEditable = false

方法2:

您可以创建UIView并将subView添加到UIWindow,这不会隐藏键盘,用户也无法键入。

var overView : UIView? // GLOBAL DECLARATION

func addOverView() { // CALL THIS WHEN SEND BUTTON CLICKED

   let window = UIApplication.shared.keyWindow!
   overView = UIView(frame: window.bounds)
   overView?.backgroundColor = UIColor.red.withAlphaComponent(0.4)

   let windowCount = UIApplication.shared.windows.count
   UIApplication.shared.windows[windowCount-1].addSubview(overView!)
}

func removeOverView() { // CALL THIS WHEN RESPONSE RECEIVED

    overView?.removeFromSuperview()
}

输出方式2:

enter image description here

方法1:

使用UTextView Delegate

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

    if isSendBtnClicked == true {

        return false
    }


    return true
}



@IBAction func sendButtonClicked(_ sender: UIButton) {

    // Disable textview and send button while message is being sent
    self.isSendBtnClicked = true
    sendButton.isEnabled = false

    fetchSendMessage( ) { (result, error) in
        if error != nil {
            ShowDialog.showDialog(title: nil, message: (error?.localizedDescription)!, viewController: self)
        } else {
            if (result?.success)! {
                self.arry = (result?.data)!

                self.fetchMessages(page: 1, completed: {
                    self.insertNewMessage(sentMessage)
                })

                // Clear the message & Enable textview and send button
                self.isSendBtnClicked = false
                self.messageTextView.text = "Type Message"
                self.messageTextView.textColor = UIColor.greyColour
                self.sendButton.isEnabled = true
                self.messageTextView.endEditing(true)
            } else {
                ShowDialog.showDialog(title: "Message not sent", message: (result?.errors![0].message)!, viewController: self)

                // Retain typed message
                // Enable textview and send button
                self.isSendBtnClicked = false
                self.sendButton.isEnabled = true
            }
        }
    }
}

一旦用户单击“发送”按钮,键盘将不会隐藏并且用户可以键入,但只有在收到fetchSendMessage()的回复后,它才能进入该文本视图