UISearchController search bar doesn't get focus

微笑、不失礼 提交于 2019-12-31 02:26:06

问题


In my project, If a controller (containing UISearchController) is pushed second time on to the navigation stack, Search bar can't seem to get focus. The following GIF recoding demonstrates what I am talking about:

As you can see, when search screen is pushed to the navigation stack the second time (by tapping the magnifying glass), I can't tap in the search bar anymore.

Code to reproduce the issue: If you'd like to look at the code, You can download the minimal project.


回答1:


You need to set your UISearchControllerDelegate

    class SearchController: UITableViewController, UISearchControllerDelegate, UISearchBarDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()

        searchController = UISearchController(searchResultsController: nil)
        searchController.delegate = self
        searchController.searchBar.delegate = self

        self.definesPresentationContext = false
    }

in the storyboard also need to change the segue from Show Push, to Show Replace

or use extensions however the cool kids are doing it now.




回答2:


Try this

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.definesPresentationContext = YES;
 }

- (void)viewDidDisappear:(BOOL)animated {
    self.definesPresentationContext = NO;
    [super viewDidDisappear:animated];
 }

it working fine for me, but not sure is it good solution or not?



来源:https://stackoverflow.com/questions/35013821/uisearchcontroller-search-bar-doesnt-get-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!