//
// CommentViewController.swift
// Bordy
//
// Created by Micheal Tyler on 9/22/17.
// Copyright © 2017 Bordy,LLC. All rights reserved.
//
import UIKit
import FirebaseDatabase
import FirebaseAuth
class CommentViewController: UIViewController {
@IBOutlet weak var commentTextField: UITextField!
@IBOutlet weak var sendButton: UIButton!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var constraintToBottom: NSLayoutConstraint!
let postId = "-KuXb-8C5kijYNqOOihB"
var comments = [Comment]()
var users = [Users]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.estimatedRowHeight = 77
tableView.rowHeight = UITableViewAutomaticDimension
empty()
handleTextField()
loadComments()
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)
}
/ *这是我放置代码的地方,当我点击模拟器中的屏幕没有打印时,如果用户点击屏幕退出textField,我的键盘也不会隐藏。 * /
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
print("12121")
}
答案 0 :(得分:0)
由于您在此处使用表格视图,因此您的表格视图的滚动视图很可能会在您的视图获取之前吸收触摸。
如果您想在用户点按表格视图时隐藏键盘,您可以考虑使用UITapGestureRecognizer
来执行此操作:
// MARK: LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleHideKeyboard)))
}
// MARK: User Interaction
func handleHideKeyboard() {
commentTextField.resignFirstResponder()
}
请注意,您可以使用resignFirstResponder()
方法隐藏键盘:
<强> resignFirstResponder()强>
通知此对象已被要求放弃其状态 作为窗口中的第一响应者。