【问题标题】:UISearchController shifted up when typing under UINavigationController在 UINavigationController 下键入时 UISearchController 向上移动
【发布时间】:2019-04-25 05:13:22
【问题描述】:

我在导航控制器中嵌入了一个视图控制器(BlueViewController)。

blueViewController 有一个 UISearchController 设置为 blueViewController 的 tableView headerView。

searchController 工作正常,但当它处于活动状态时,它会向上移动并隐藏在 UINavigationController(或某处)后面

我尝试将搜索控制器的 searchBar 作为子视图添加到 viewController,或者定位一个 UIView 并将 searchBar 分配给该 UIView,这些都不起作用

我试过这个链接,不起作用 Strange UISearchDisplayController view offset behavior in iOS 7 when embedded in navigation bar

这是 BlueViewController 中的 searchController

let searchController:UISearchController = {
    let controller = UISearchController(searchResultsController: nil)
    controller.hidesNavigationBarDuringPresentation = false
    controller.obscuresBackgroundDuringPresentation = false
    controller.searchBar.inputAssistantItem.leadingBarButtonGroups = []
    controller.searchBar.inputAssistantItem.trailingBarButtonGroups = []
    return controller
}()

在 viewDidLoad 中

 searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    definesPresentationContext = true

您还可以在此处找到整个项目 https://github.com/QiquanLu/TestNavigationWithSearchController

任何提示将不胜感激,谢谢!

【问题讨论】:

    标签: swift xcode


    【解决方案1】:

    对于 iOS 11 及更高版本,您应该像这样在 BlueViewController 的 navigationItem 上设置 searchController 属性:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        navigationItem.searchController = searchController
    }
    

    不要将其添加为 tableView 的标题。

    【讨论】:

    • 谢谢,但导航项不是我想要的搜索栏。
    • 啊,我明白了。不以这种方式使用搜索控制器可能最终会出现像您所看到的那样的奇怪错误。抱歉,我无法提供更多帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多