IOS8 UISearchController直接显示搜索栏

Pie*_*rre 3 uinavigationcontroller ios uisearchcontroller

我正在尝试使用UISearchController为我的iphone应用程序显示一个简单的搜索屏幕.我使用了UITableViewControllerUISearchController,它嵌入在UINavigationController中

  • 这让我看到以下屏幕(状态1)

带导航栏的searchcontroller

  • 当用户点击搜索字段时,我会看到下面的屏幕(状态2):搜索栏在状态栏下方的屏幕顶部显示.

searchcontroller已激活

我想做的是直接获得状态2而不进入状态1

我试图添加此代码

presentViewController(searchController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

但它要么不显示搜索栏,如果动画是假的,或者显示它我想要但是在不必要的过渡后暂时显示导航栏

Pie*_*rre 7

好的,我刚刚发现如何在UIViewController中读取这个答案UISearchController

我只需要像这样添加搜索栏以获得所需的效果

    searchController.hidesNavigationBarDuringPresentation = false
    // tableView.tableHeaderView = searchController.searchBar
    navigationItem.titleView = searchController.searchBar
Run Code Online (Sandbox Code Playgroud)