UISearchController won't dismiss searchbar and overlap for iOS 8 Swift

元气小坏坏 提交于 2019-12-08 16:33:29

问题


I'm having problem with the UISearchController where if I have text in the searchbar and dismiss the VC it's in, the searchBar won't go away and just remain on screen overlapping everything in other VCs. Then it crashes if you hit the cancel button.

Have tried a few solutions on SO but none have worked. :/

self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.hidesNavigationBarDuringPresentation = false
        controller.searchBar.sizeToFit()

        controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
        controller.searchBar.barTintColor = UIColor(red: 243/255, green: 243/255, blue: 243/255, alpha: 1)
        controller.searchBar.tintColor = UIColor.blackColor()

        controller.definesPresentationContext = true
        controller.edgesForExtendedLayout = UIRectEdge.None
        self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0)

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

Really appreciate any help on this!

UPDATE: So I kinda found a not-so-great solution which is to set .active = false in viewWillDisappear. However, the problem is there a searchBar artifact will show on the next/previous VC for a second before completely disappearing.


回答1:


Change the following:

controller.definesPresentationContext = true

to

self.definesPresentationContext = true

The setting also preserves the state of the search bar when you push and pop a view controller on and off the navigation stack.

You can read about the definesPresentationContext setting on Apple's Documentation on UIViewController.




回答2:


If you're able to use the self.definesPresentationContext = true then that's probably a better way of doing it. In my configuration I was fixing another bug with UISearchController by setting definesPresentationContext to false.

So instead I switched from calling

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.searchController setActive:FALSE];
}

to:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [super prepareForSegue:segue sender:sender];
    [self.searchController setActive:FALSE];
}

and that seemed to do the trick.



来源:https://stackoverflow.com/questions/29830698/uisearchcontroller-wont-dismiss-searchbar-and-overlap-for-ios-8-swift

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