【问题标题】:While typing on keyboard button tap doesn't get recognized在键盘按钮上打字时无法识别
【发布时间】:2018-02-11 06:39:17
【问题描述】:

我的 iOS 应用程序有问题。我想实现类似MessagingViewController 的东西,在UITableView 下方有UITextViewButton (sendButton)。如果点击 textView,则会出现键盘,View 会上升。到目前为止,一切都很好。

但是,如果我在输入时输入随机文本并按标签/按发送按钮(与现在应该发生的情况无关),或者如果输入和按钮标签之间的时间间隔很小,则无法识别点击。如果您尝试 iMessage 或 WhatsApp,这不是问题。

我不知道该怎么办,我也试过像SlackViewControllerInputAccessoryView 这样的CocoaPods,但总是同样的问题。键入时,无法识别按钮点击。我用UIButtonUITapGestureRecognizer 的普通IBAction 进行了尝试。

我希望有人能帮助我,这个问题让用户体验很糟糕。

非常感谢!!!

编辑:这是一个按钮位于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
        }
    }
}

}

【问题讨论】:

  • 在此处或类似 github 的地方添加您尝试过的代码。

标签: ios swift uibutton uitextfield uitapgesturerecognizer


【解决方案1】:

我刚刚在我的项目上进行了测试,它没有任何问题,添加你的代码来看看你做了什么。 (我无法发表评论,所以我把它作为答案)

【讨论】:

  • 效果很好……你可以把你的项目发给我,我可以测试一下。
  • 对不起,没有,你是在手机上测试还是在模拟器上测试?例如,您可以在那里检查可可豆荚“SlackViewController”,您遇到了同样的问题。发送按钮在键入时没有反应。在我的示例中,它只是一个单视图应用程序,我所拥有的只是我发布的视图控制器加上故事板文件..故事板和视图控制器之间的所有连接都已建立。
  • 好的,我发现它可以在 iPhone 上完美运行,但在 iPad 上却不行,但我不知道为什么
猜你喜欢
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
  • 2013-02-19
相关资源
最近更新 更多