前提・実現したいこと
アプリの検索機能をAlgoliaを用いて実装しました。
textFieldを監視して、入力に変化があれば検索結果をリアルタイムで検索をして検索結果をtableViewに表示しています。
textFieldに適当に文字列を入力して、全ての文字列を消すと勝手にKeyBoardが閉じてしまいます。
なぜかよくわからないのですが、1文字ずつ削除していくとなぜか最後の1文字までいくと、最後1文字を残してをKeyBoardが閉じるパターンも有ります。
keyboardの挙動ついてよく理解できていませんので、ご教授お願いします。
結論rkeyBoardが勝手に閉じるのを防ぎたいです。
該当のソースコード
import UIKit import NVActivityIndicatorView class SearchViewController: UIViewController, UITextFieldDelegate { //MARK: - IBOutlets @IBOutlet weak var tableView: UITableView! @IBOutlet weak var searchOptionView: UIView! @IBOutlet weak var searchTextField: UITextField! @IBOutlet weak var searchButtonOutlet: UIButton! //MARK: - Vars var searchResults: [Item] = [] var activityIndicator: NVActivityIndicatorView? //MARK: - Constraints @IBOutlet weak var searchOptionViewHeightConstraint: NSLayoutConstraint! //MARK: - View LifeCycle override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.opaqueSeparator tableView.tableFooterView = UIView() setupUI() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default) self.navigationController!.navigationBar.shadowImage = UIImage() self.navigationController!.navigationBar.backgroundColor = UIColor.opaqueSeparator } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) activityIndicator = NVActivityIndicatorView(frame: CGRect(x: self.view.frame.width/2 - 30, y: self.view.frame.width/2 - 30, width: 60, height: 60), type: .lineScale, color: #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1), padding: nil) } //MARK: - IBActions @IBAction func showSearchBarButtonPressed(_ sender: Any) { dismissKeyBoard() emptyTextField() showSearchField() } @IBAction func searchButtonPressed(_ sender: Any) { showLoadingIndicator() if searchTextField.text != "" { print("SearchButton Taped search Text : (searchTextField.text!)") searchInFireStore(forName: searchTextField.text!) emptyTextField() animateSearchOptionsIn() DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) { self.hideLoadingIndicator() } } } //MARK: - Search Database private func searchInFireStore(forName: String) { searchAlgolia(searchString: forName) { (itemIds) in downloadItems(withIds: itemIds) { (allItems) in self.searchResults = allItems self.tableView.reloadData() } } } //MARK: - Helpers private func emptyTextField() { searchTextField.text = "" } private func dismissKeyBoard() { self.view.endEditing(true) } @objc func textFieldDidChange(_ textField: UITextField) { searchButtonOutlet.isEnabled = textField.text != "" if searchButtonOutlet.isEnabled{ searchButtonEditingAnimation() }else{ searchButtonNotEditAnimation() } if searchTextField.text != "" { searchInFireStore(forName: searchTextField.text!) }else{ self.searchResults.removeAll() self.tableView.reloadData() } } private func disableSearchButton(){ searchButtonOutlet.isEnabled = false } private func showSearchField() { disableSearchButton() emptyTextField() animateSearchOptionsIn() } private func searchTextFieldAddUnderLine() { let underLine = UIView() underLine.backgroundColor = UIColor(red: 0.32, green: 0.65, blue: 0.91, alpha: 1.0) underLine.frame = CGRect(x: 0, y: self.searchTextField.frame.height, width: self.searchTextField.frame.width, height: 2.5) searchTextField.borderStyle = .none self.searchTextField.addSubview(underLine) } //MARK: - Animations private func animateSearchOptionsIn() { UIView.animate(withDuration: 0.4) { self.searchOptionView.isHidden = !self.searchOptionView.isHidden if self.searchOptionView.isHidden == true { self.searchTextField.resignFirstResponder() }else{ self.searchTextField.becomeFirstResponder() } } } private func searchButtonEditingAnimation() { UIView.animate(withDuration: 0.4) { self.searchButtonOutlet.titleLabel?.textColor = UIColor.systemGreen self.searchButtonOutlet.isEnabled = true } } private func searchButtonNotEditAnimation() { UIView.animate(withDuration: 0.4) { self.disableSearchButton() self.dismissKeyBoard() } } //MARK: - Activity Indicator private func showLoadingIndicator() { if activityIndicator != nil { self.view.addSubview(activityIndicator!) activityIndicator!.startAnimating() } } private func hideLoadingIndicator() { if activityIndicator != nil { activityIndicator!.stopAnimating() activityIndicator!.removeFromSuperview() } } private func showItemView(withItem: Item) { let itemVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "itemVC") as! ItemViewController itemVC.item = withItem self.navigationController?.pushViewController(itemVC, animated: true) } //MARK: - Setup UI private func setupUI() { searchTextFieldSetup() searchOptionViewSetup() searchButtonSetup() } private func searchOptionViewSetup() { searchOptionView.layer.cornerRadius = 12.0 searchOptionView.layer.maskedCorners = [.layerMinXMaxYCorner,.layerMaxXMaxYCorner] } private func searchTextFieldSetup() { searchTextField.delegate = self searchTextFieldAddUnderLine() searchTextField.attributedPlaceholder = NSAttributedString(string: "Search...", attributes: [NSAttributedString.Key.foregroundColor: UIColor.darkGray]) searchTextField.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: UIControl.Event.editingChanged) } private func searchButtonSetup() { searchButtonOutlet.layer.cornerRadius = 12.0 searchButtonOutlet.layer.shadowColor = UIColor.black.cgColor searchButtonOutlet.layer.shadowOffset = CGSize(width: 0, height: 0) searchButtonOutlet.layer.shadowRadius = 8.0 searchButtonOutlet.layer.shadowOpacity = 0.13 } } //MARK: - Extension UITableView Delegate And DataSource extension SearchViewController: UITableViewDelegate, UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return searchResults.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ItemTableViewCell cell.toFields(searchResults[indexPath.row]) return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) showItemView(withItem: searchResults[indexPath.row]) } }
開発環境
swift5
xcode 11
回答1件
あなたの回答
tips
プレビュー