虽然在键盘按键上敲击不会被识别

虽然在键盘按键上敲击不会被识别

问题描述:

我的iOS应用程序出现问题。我想实现类似于MessagingViewController的地方,在UITableView以下有UITextViewButton(sendButton)。如果点击textView,键盘出现,视图上升..到目前为止这么好。虽然在键盘按键上敲击不会被识别

但是,如果我正在输入随机文本和选项卡/按发送按钮(与现在应该发生什么无关),或者如果打字和按钮选项卡之间的时间间隔太小,则无法识别水龙头。如果您尝试iMessage或WhatsApp,这不是问题。

我不知道该怎么做,我也尝试了可可豆,如SlackViewControllerInputAccessoryView但它总是同样的问题。在输入时,按钮点击不会被识别。我试用UIButtonUITapGestureRecognizer的正常IBAction

我希望有人能够帮助我,这个问题使得UX变得可怕。

非常感谢!

编辑:下面是一个示例,其中Button在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 
     } 
    } 
} 

}

+0

添加您的尝试代码在这里或像github。 –

我只是在我的项目进行测试,它的工作没有任何问题,请将您的代码,看看你做了什么。 (我不能评论,所以我把这作为一个答案)

+0

我加了一个例子,谢谢! –

+0

它的功能非常完美...您可以将您的项目发送给我,我可以测试它。 –

+0

对不起,它没有,你在手机或模拟器上测试过吗?例如,你可以签出可可豆荚“SlackViewController”那里你有同样的问题..发送按钮不反应,而打字..在我的例子中,它只是一个单一的视图应用程序,我拥有的是我发布的viewcontroller加上故事板文件。故事板和视图控制器之间的所有连接都已完成。 –