【问题标题】:UISearchControllerDelegate - Search bar not visible in table headerUISearchControllerDelegate - 搜索栏在表头中不可见
【发布时间】:2014-06-09 12:21:39
【问题描述】:

我的UITableViewController 符合新的UISearchControllerDelegateUISearchResultsUpdating

这是我的搜索栏设置代码:

override func viewDidLoad() {
    var searchController = UISearchController(searchResultsController: self)
    searchController.searchResultsUpdater = self
    self.tableView.tableHeaderView = searchController.searchBar
    self.definesPresentationContext = true
}

但是,当在模拟器中运行时,表格标题中没有搜索栏,即使它在代码中指定。我也在viewWillAppear 中尝试过这段代码,但再次没有显示搜索栏。

【问题讨论】:

    标签: ios uitableview swift


    【解决方案1】:

    一位 Apple 工程师通知我,您必须为搜索栏添加一个框架。如果您打印搜索栏的框架,您会注意到它的高度为零。所以这可能是苹果代码中的一个错误。

    searchController.searchBar = CGRectMake(0.0, 0.0, 320.0, 44.0)
    

    编辑:

    文档指定必须传入要显示结果的 View Controller。要在您所在的同一 View Controller 中显示它,请传入 nil。

    var searchController = UISearchController(searchResultsController: nil)
    

    【讨论】:

    • 谢谢,现在显示了。但是,当我点击它2014-06-10 11:23:34.410 MyApp[21108:1196461] Warning: Attempt to present <UISearchController: 0x10c2378b0> on <_TtC8MyApp22ViewController: 0x10c21cc50> whose view is not in the window hierarchy! 时出现错误并且屏幕是黑色的。有什么想法吗?
    • 您也可以在searchBar 上使用sizeToFit,而不是设置显式框架。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多