How do you dismiss a UISearchController ? (iOS 8 and follow)

前端 未结 5 562
野性不改
野性不改 2020-12-25 10:53

This must be trivial, but I can\'t find how you\'re supposed to dismiss a UISearchController programmatically?

Note that it\'s the new UISearchController (introduced

相关标签:
5条回答
  • 2020-12-25 11:05

    SWIFT 4+

    searchController.isActive = false
    
    0 讨论(0)
  • 2020-12-25 11:06

    I had this problem using the search and interactionController, solved after just include the line: self.dismissViewControllerAnimated(false, completion: nil)

    Open the interaction and clear the search without changes in the delegate.

    0 讨论(0)
  • 2020-12-25 11:14

    OK so after more testing, turns out you just have to set:

    searchController.active = false
    // or swift 4+
    searchController.isActive = false
    

    This is the first thing I tried but I called it in one of the UISearchControllerDelegate methods which didn't work (probably should have called it with dispatch_async (halbano's answer seems to confirm that)).

    Anyway, since I couldn't find that answer online, I'm answering my own question, I hope that it'll help someone.

    0 讨论(0)
  • 2020-12-25 11:22

    I was presenting the mine embed on a navigation bar. The code that works for me was:

    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.searchController setActive:NO];
            self.navigationController.navigationBar.topItem.title = @"MYTITLE".uppercaseString;
            self.navigationItem.titleView = nil;
        });
    }
    

    Hope it helps someone.

    0 讨论(0)
  • 2020-12-25 11:29

    Did you have this problem when you try to dismiss search controller after segueing to another view? I have encountered this problem too. I think you might want to use

    self.definesPresentationContext = true 
    

    in the view controller that presents the UISearchController as per this post UISearchController not dismissed when View is pushed. It works for me.

    0 讨论(0)
提交回复
热议问题