在键盘上键入按钮时,无法识别

时间:2017-09-02 11:00:42

标签: ios swift uibutton uitextfield uitapgesturerecognizer

我的iOS应用程序出了问题。我希望实现像MessagingViewController这样的内容,其中UITextViewButton(sendButton)位于UITableView下方。如果点击textView,键盘出现,View就会上升..到目前为止一直很好。

但是,如果我输入随机文本和制表符/按下发送按钮(独立于现在应该发生的事情),或者如果键入和按钮选项卡之间的时间间隔很小,则点击不会得到认可。如果您尝试使用iMessage或WhatsApp,这不是问题。

我不知道该怎么做,我也试过像SlackViewControllerInputAccessoryView这样的CocoaPods,但它始终是同样的问题。键入时,按钮点击不会被识别。我使用IBActionUIButton的正常UITapGestureRecognizer进行了尝试。

我希望有人可以帮助我,这个问题使得用户体验非常糟糕。

非常感谢!!!

编辑:这是按钮位于InputAccessoryView的示例。

import UIKit

class MessagingViewController: UIViewController, UITableViewDataSource {

var messages: [String] = ["12", "32"]
var accessory: UIView!
var cancelButton: UIButton!
var charactersLeftLabel: UILabel!
var sendButton: UIButton!


@IBOutlet var tableView: UITableView!
@IBOutlet var messagesTextView: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.dataSource = self
    tableView.register(UINib(nibName: TableViewCell.className, bundle:nil),
                       forCellReuseIdentifier: TableViewCell.className)
    addAccessoryView()

    NotificationCenter.default.addObserver(self, selector: #selector(MessagingViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(MessagingViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return messages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as? TableViewCell

    cell?.titleLabel.text = "Sender: \(indexPath.row): "
    cell?.detailLabel.text = messages[indexPath.row]

    return cell!
}

func addAccessoryView() {
    let frame = CGRect(x:0, y:0, width: self.view.frame.width,height: 45)
    accessory = UIView(frame: frame)
    accessory.backgroundColor = UIColor.lightGray
    accessory.alpha = 0.6
    accessory.translatesAutoresizingMaskIntoConstraints = false
    self.messagesTextView.inputAccessoryView = accessory


    sendButton = UIButton(type: .custom)
    sendButton.setTitleColor(UIColor.red, for: .normal)
    sendButton.setTitle("Send", for: .normal)
    sendButton.setTitleColor(UIColor.white, for: .disabled)
    sendButton.addTarget(self, action: #selector(MessagingViewController.sendButtonTapped(_:)), for: .touchUpInside)
    sendButton.showsTouchWhenHighlighted = true
    sendButton.isEnabled = true
    sendButton.translatesAutoresizingMaskIntoConstraints = false
    accessory.addSubview(sendButton)
    let sendTrailingConstraint = NSLayoutConstraint(item: sendButton, attribute: .trailing, relatedBy: .equal, toItem: accessory, attribute: .trailing, multiplier: 1.0, constant: -20)
    accessory.addConstraint(sendTrailingConstraint)
    let sendCenterYConstraint = NSLayoutConstraint(item: sendButton, attribute: .centerY, relatedBy: .equal, toItem: accessory, attribute: .centerY, multiplier: 1.0, constant: 0)
    accessory.addConstraint(sendCenterYConstraint)
}

func sendButtonTapped(_ sender: UIButton) {
    messages.append(messagesTextView.text)
    messagesTextView.text.removeAll()

    tableView.reloadData()
    tableView.scrollToRow(at: IndexPath(row: messages.count - 1, section: 0), at: .bottom, animated: true)
}

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

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

}

1 个答案:

答案 0 :(得分:0)

我刚刚测试了我的项目,它没有任何问题,添加你的代码看看你做了什么。 (我不能评论所以我把它作为答案)