【问题标题】:tvOS UISearchController - how avoid automatic SearchBar & Keyboard hidden behavior when scroll?tvOS UISearchController - 滚动时如何避免自动搜索栏和键盘隐藏行为?
【发布时间】:2020-05-14 15:34:43
【问题描述】:

在我的应用程序中,我按照为 Apple 提供的示例项目 UIKitCatalog 实现了 UISearchController,代码如下:

    /*
         A method demonstrating how to encapsulate a `UISearchController` for presentation in, for example, a `UITabBarController`
    */
    func packagedSearchController() -> UIViewController {
        // Load a `SearchResultsViewController` from its storyboard.
        let storyboard = UIStoryboard(name: "ViewControllerSamples", bundle: nil)
        guard let searchResultsController = storyboard.instantiateViewController(withIdentifier: SearchResultsViewController.storyboardIdentifier) as? SearchResultsViewController else {
            fatalError("Unable to instatiate a SearchResultsViewController from the storyboard.")
        }

        /*
            Create a UISearchController, passing the `searchResultsController` to
            use to display search results.
        */
        let searchController = UISearchController(searchResultsController: searchResultsController)
        searchController.searchResultsUpdater = searchResultsController
        searchController.searchBar.placeholder = NSLocalizedString("Enter keyword (e.g. iceland)", comment: "")

        // Contain the `UISearchController` in a `UISearchContainerViewController`.
        let searchContainer = UISearchContainerViewController(searchController: searchController)
        searchContainer.title = NSLocalizedString("Search", comment: "")

        // Finally contain the `UISearchContainerViewController` in a `UINavigationController`.
        let searchNavigationController = UINavigationController(rootViewController: searchContainer)
        return searchNavigationController
    }

此实现在我的应用程序中运行良好,但具有想要避免的 SearchBar 和键盘的自动隐藏行为,这可能吗?以及如何?

【问题讨论】:

  • 你找到解决办法了吗??
  • 不,实际上我的 tvOS 应用程序存在这种行为。
  • 对于其他页面,我可以接受 tvOS 的默认行为。但是对于我的应用程序的搜索页面,我不希望搜索栏和键盘自动隐藏。 Youtube 正在这样做。他们的搜索栏和键盘不会隐藏。我需要同样的行为

标签: uikit uisearchbar tvos uisearchcontroller uikeyboard


【解决方案1】:

我经常搜索这个问题,我发现的只是覆盖变量disablesAutomaticKeyboardDismissal: Bool并使其返回true,但它没有工作

对我来说唯一成功的解决方案是强制关注搜索栏

class CustomeSearchController : UISearchController {
    
    override var preferredFocusEnvironments: [UIFocusEnvironment]{
        return [self.searchBar]
    }
        
    override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        self.view.setNeedsFocusUpdate()
        self.view.updateFocusIfNeeded()
    }
}

希望对你有帮助

【讨论】:

  • 但这会阻止其他元素获得焦点对吧??
  • @pgokul,不幸的是,是的,所以你需要检查didUpdateFocus 中的下一个元素是什么,并通过全局变量将preferredFocusEnvironments 传递给它,以防万一你想移动到那个元素,无论如何在我的项目中,我决定采用现有的行为,并在失去焦点时让它隐藏起来,因为即使使用这种解决方案,您也会面临一些故障,并且需要修复更多错误
  • @AhmedAdnanQazzaz 嗨,Ahmed,很高兴在那里见到你 :) 当包含 SearchController 时,如何避免在 SearchVC 上滚动时隐藏 Tabbar?
猜你喜欢
  • 2020-04-11
  • 1970-01-01
  • 2013-06-02
  • 1970-01-01
  • 2011-05-22
  • 2011-03-08
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
相关资源
最近更新 更多