2017-09-02 70 views
0

我的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

它的功能非常完美...您可以將您的項目發送給我,我可以測試它。 –

+0

對不起,它沒有,你在手機或模擬器上測試過嗎?例如,你可以簽出可可豆莢「SlackViewController」那裏你有同樣的問題..發送按鈕不反應,而打字..在我的例子中,它只是一個單一的視圖應用程序,我擁有的是我發佈的viewcontroller加上故事板文件。故事板和視圖控制器之間的所有連接都已完成。 –