【问题标题】:Cannot get becomeFirstResponder work on UISearchController无法在 UISearchController 上获得 becomeFirstResponder 工作
【发布时间】:2018-08-18 00:04:39
【问题描述】:

viewDidLoad():

searchController = UISearchController(searchResultsController: nil)
let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)

searchController?.dimsBackgroundDuringPresentation = false
searchController?.searchResultsUpdater = self
searchController?.delegate = self
searchController?.searchBar.tintColor = UIColor.black
searchController?.searchBar.barTintColor = UIColor(white: 0.9, alpha: 0.9)
searchController?.searchBar.placeholder = NSLocalizedString("Search", comment: "")
searchController?.hidesNavigationBarDuringPresentation = false
self.navigationItem.titleView = self.searchController?.searchBar

searchController?.isActive = true
self.searchController?.becomeFirstResponder()
self.searchController?.searchBar.becomeFirstResponder()

这是委托方法:

extension SearchMembers :  UISearchControllerDelegate {
    func didPresentSearchController(searchController: UISearchController){
          self.searchController?.searchBar.becomeFirstResponder()
    }
}

在我的测试中,didPresentSearchController(searchController:) 从未被调用过。

我不知道为什么,但无论我做什么都是行不通的。

有什么想法吗?

编辑 唯一有效的解决方案

      delay(0.6) {
         self.searchController?.searchBar.becomeFirstResponder()
        }

这会造成不必要的延迟

【问题讨论】:

标签: ios swift uisearchcontroller


【解决方案1】:

根据didPresentSearchControllerdocumentation

该方法仅在搜索控制器自动呈现时调用。如果您明确显示搜索控制器,则不会调用它。

如果您想显式显示搜索结果界面,请将搜索控制器包装在 UISearchContainerViewController 对象中并改为显示该对象。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-14
相关资源
最近更新 更多