ios - UISearchController - 键盘不显示

标签 ios uitableview swift uisearchcontroller

我正在使用 UISearchController 并且搜索功能可以正常工作,但有两点除外。也许它们是相关的: a) 键盘不显示。所以我不能按“搜索”按钮 b) 在我的主 TableView 中,我访问了具有“副标题”样式的原型(prototype)单元格。进行搜索时,显示的单元格为“基本”样式。在这两种情况下(TableViewController 和 SearchViewController)我都使用相同的单元格标识符。

有什么想法吗?这里有一些代码:

class OverviewTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let resultsController = SearchResultsController()
        resultsController.birds = birds
        searchController = UISearchController(searchResultsController: resultsController)
        //searchController.dimsBackgroundDuringPresentation = true

        let searchBar = searchController.searchBar
        searchBar.scopeButtonTitles = ["One", "Two"]
        searchBar.placeholder = "Search"
        searchBar.sizeToFit()
        tableView.tableHeaderView = searchBar
        searchController.searchResultsUpdater = resultsController

第二类:

class SearchResultsController: UITableViewController, UISearchResultsUpdating {

    var items: [Item] = []
    var filtered: [Item] = []

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

// MARK: - UISearchResultsUpdating Conformance

func updateSearchResultsForSearchController(searchController: UISearchController) {
    //searchController.hidesNavigationBarDuringPresentation = true
    let searchString = searchController.searchBar.text
    let buttonIndex = searchController.searchBar.selectedScopeButtonIndex
    filtered.removeAll(keepCapacity: true)

    if !searchString.isEmpty {
        for item in items {
            ...
            }
        }

    self.tableView.reloadData()

    }
}

最佳答案

我有同样的问题,基本上如果你的父类有一个 UISearchController 以及你的子类并且你正在设置

self.definesPresentationController = YES

在您的父类中,子类的搜索 Controller 不会显示键盘。我花了一段时间才弄明白,但这就是正在发生的事情。

解决方法是在你的父类要push子类的时候设置self.definesPresentationController = NO,在你父类的 viewWillAppear...

希望这也能帮助其他人。

关于ios - UISearchController - 键盘不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30491613/

相关文章:

ios - UITextField - UIView 以编程方式调整大小

ios - 将随机元素添加到数组(swift)

swift - 自定义单元格中的 didSelectRowAt 不执行任何操作

swift - 在 Swift 中没有 nib 文件的 NSWindowController

iphone - 如何在 UITableView 之上添加一个 UIImage

ios - 解析文件图像未加载到 PFTableViewCell

ios - 如何在 Swift 中使用 IAP 正确恢复购买

ios - react-native-push-notification 在 ios 设备上不起作用

ios - 为 UITableView 部分标题返回 CGFloat.leastNormalMagnitude 会导致崩溃

uitableview - Swift:如何使用“编辑”按钮删除并停用滑动以删除?