IOS8 UISearchController 直接显示搜索栏

标签 ios uinavigationcontroller uisearchcontroller

我正在尝试使用 UISearchController 为我的 iPhone 应用程序显示一个简单的搜索屏幕。 我使用了一个 UITableViewController 和一个嵌入到 UINavigationController

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

searchcontroller with navigation bar

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

searchcontroller activated

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

我尝试添加这段代码

presentViewController(searchController, animated: true, completion: nil)

但如果 animated 为 false,它要么不显示搜索栏,要么按我的意愿显示它,但在经过不必要的过渡后暂时显示导航栏

最佳答案

好的,我刚刚找到如何阅读这个答案 UISearchController in a UIViewController

我只需要像这样添加搜索栏就可以得到想要的效果

    searchController.hidesNavigationBarDuringPresentation = false
    // tableView.tableHeaderView = searchController.searchBar
    navigationItem.titleView = searchController.searchBar

关于IOS8 UISearchController 直接显示搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27867611/

相关文章:

ios - UISearchController 未按预期显示

iphone - 如何在 Iphone 中单击 UIButton 时设置 UiScrollView Frame?

ios - 无法排序我的NSMutablearray

iOS - 带有推送通知的 App Store 提交

uinavigationcontroller - ios7在导航堆栈中返回的新平移手势不会清除tableview选择

ios - 为什么 View Controller 的标题不居中,即使我使用简单的(self.title = "title")

ios - 从 UIViewController 调用自定义 UINavigationController 中的方法时目标错误

ios - 删除大标题 UINavigationBar 中 UISearchController 顶部的 1px 行

ios - 如何在没有底层 UIScrollView 的情况下在 UINavigationItem 上显示 UISearchController?

android - 在 iOS 和 Android 上处理推送通知场景