【问题标题】:UIsearchController not becoming first responder in ios 9UIsearchController 没有成为 ios 9 中的第一响应者
【发布时间】:2015-10-28 09:10:19
【问题描述】:

我在导航栏中有一个UISearchController,它一直运行良好。

仅在 iOS 9 上,搜索栏在控制器加载后拒绝成为第一响应者。

viewDidLoad中的代码:

self.searchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.delegate = self

        controller.dimsBackgroundDuringPresentation = false

        let ownSearchBar = controller.searchBar
        ownSearchBar.searchBarStyle = .Default
        ownSearchBar.barStyle = UIBarStyle.Black
        ownSearchBar.placeholder = NSLocalizedString("Search",  comment: "Search")
        ownSearchBar.showsCancelButton = true
        ownSearchBar.sizeToFit()
        ownSearchBar.delegate = self

        controller.searchBar.delegate = self
        controller.hidesNavigationBarDuringPresentation = false

        self.navigationItem.titleView = controller.searchBar
        self.navigationItem.hidesBackButton = true
        self.navigationController?.navigationBar.sizeToFit()


        return controller

    })()

viewDidAppear 中的代码:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.searchController.active = true
    self.searchController.searchBar.text = state.term
    self.segmentedControl.selectedSegmentIndex = state.searchType
}

还有我在didPresentSearchController中的代码:

extension SearchController :  UISearchControllerDelegate {
func didPresentSearchController(searchController: UISearchController){
    self.searchController.searchBar.becomeFirstResponder()
    xButtonWasPressed = true
}

}

请帮我找出为什么只有在 iOS 9 中搜索栏没有获得焦点

【问题讨论】:

  • 你在看模拟器还是readl设备?
  • 我都签入了。设备是 iPhone 6
  • 对我来说同样的问题。认为这是一个错误...... :(
  • 也得到了这个错误,在 iOS8 中一切正常 :(

标签: swift uisearchbar ios9 uisearchcontroller becomefirstresponder


【解决方案1】:

对我来说同样的问题。在 iOS 8 上运行良好,但在 iOS 9 上停止运行。

错误是在 didPresentSearchController: 方法中应该是 SearchController 已经加载但它不是。

所以我们需要延迟加载它。我丑陋但有效的解决方案是

 func didPresentSearchController(searchController: UISearchController) {

    UIView.animateWithDuration(0.1, animations: { () -> Void in }) { (completed) -> Void in

         searchController.searchBar.becomeFirstResponder()
    }
}

becomeFirstResponder() 在这个方法的 completed 部分中非常重要,因为它是 SearchController 加载时的部分

希望对你有帮助!

【讨论】:

    【解决方案2】:

    有用的东西...

    1. 在 viewDidLoad 中设置 self 为搜索栏的代理 searchController.delegate = self

    2. 在 viewDidAppear 中设置self.searchController.isActive = true。如果您在 viewWillAppear 中执行此操作,它可能无法立即工作

    3. 使用 UISearchControllerDelegate 方法激活键盘

    
        func presentSearchController(_ searchController: UISearchController) {
            DispatchQueue.main.async {
                    searchController.searchBar.becomeFirstResponder()
                }
        }
    }
    

    注意:有一个名为 didPresentSearchController 的方法,您认为可以使用它,但只有在系统自动显示搜索栏时才会触发。

    【讨论】:

      【解决方案3】:

      使用以下代码:

      override func viewDidAppear(_ animated: Bool) {
              super.viewDidAppear(animated)            
              self.countrySearchBar.becomeFirstResponder()
          }
      

      这里的“countrySearchBar”是你的 searchBar 的出口。

      请查看我的 GitHub 链接以测试示例项目:

      https://github.com/k-sathireddy/SearchDisplayControllerSample

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 2016-10-26
        • 1970-01-01
        相关资源
        最近更新 更多