UISearchController
中有一个UISearchBar
NavigationBarTitleView
。当您开始输入SearchBar
时,会出现ResultsTableView
。键入时SearchBar
旁边会显示一个取消按钮,它会正确地取消TableView
。到现在为止还挺好。
Scene的主viewController是一个TableViewController
,它连接到NavigationController
中的TabBarController
。
现在,一切正常,但是当显示ResultsTableView
并且您离开该屏幕时(通过tabBar
),当您返回时,屏幕会变黑。
我想我需要在导航时忽略resultsTableView
以防止这种情况(就像我触发结果时的情况一样),但我无法使其发挥作用。
这里有一些代码:
class PCFeedTableViewController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating, UISearchControllerDelegate {
let searchController = UISearchController(searchResultsController: PCSearchTableViewController())
func updateSearchResultsForSearchController(searchController: UISearchController) {
let resultsTable = searchController.searchResultsController as! PCSearchTableViewController
resultsTable.tableView.frame.origin = CGPoint(x: 0, y: 64)
resultsTable.tableView.frame.size.height = self.tableView.frame.height - 113
resultsTable.tableView.tableHeaderView = nil
resultsTable.tableView.contentInset = UIEdgeInsetsZero
resultsTable.tableView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
resultsTable.parentController = self
resultsTable.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "searchResultCell")
}
...
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.scrollsToTop = true
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit()
self.searchController.searchBar.placeholder = "Search for Movies or TV Shows"
self.searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
self.searchController.searchBar.barStyle = UIBarStyle.Black
UILabel.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.lightTextColor()
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.whiteColor()
self.navigationItem.titleView = searchController.searchBar
}
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationViewController = segue.destinationViewController as! PCMainDetailsTableViewController
self.searchController.searchResultsController?.dismissViewControllerAnimated(true, completion: nil)
self.searchController.searchBar.text = nil
destinationViewController.title = searchResult.title
destinationViewController.movie = searchResult
}
}
答案 0 :(得分:1)
尝试在viewDidDisappear:
中放置用于解除搜索控制器的代码,如下所示:
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.searchController.searchResultsController?.dismissViewControllerAnimated(true, completion: nil)
self.searchController.searchBar.text = nil
}
答案 1 :(得分:1)
好的......经过相当多的摆弄,我能够解决它。
问题是切换标签时不会调用viewWillDisappear:
和viewDidDisappear:
。
要解决此问题,我必须为TabBarController
创建一个新课程,并覆盖didSelectItem:
方法以调用viewDidDisappear
PCFeedTableViewController
方法