UISearchController: overlapping searchBar and scopeBar on first touch event

I am trying to implement a simple search bar using Apple's newest UISearchController. ... However, I cannot get it to work correctly if I use the visibility bar in the search bar to get the filter selection,

The area line always shows which I could live with at startup scope bar always shows

but in the very first touch event , the search bar and visibility bar overlap. Search bar and scopebar collapse

I used the Apple TableView sample app but didn't change anything.

- (void)viewDidLoad {
[super viewDidLoad];

_resultsTableController = [[APLResultsTableController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.searchController.searchBar.showsScopeBar = YES;
[self.searchController.searchBar setScopeButtonTitles:@[@"First",@"Second"]];
self.tableView.tableHeaderView = self.searchController.searchBar;

// we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
self.resultsTableController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others

// Search is now just presenting a view controller. As such, normal view controller
// presentation semantics apply. Namely that presentation will walk up the view controller
// hierarchy until it finds the root view controller or one that defines a presentation context.
//
self.definesPresentationContext = YES;  // know where you want UISearchController to be displayed

      

}

I've implemented the methods to delegate the search bar methods , but it just toggles the overlap time.

Has anyone been able to implement a search bar using the iOS 8s UISearchController and use the SearchBar visibility?

Thank you in advance

+3


source to share


3 answers


I've only been working on this same issue for three days, going through all the posts I could find, and finally found a solution on the Apple Dev forum, so I thought I'd post an update to help the next person facing this issue.

The resolution is to make sure the showScopeBar property on the search bar owned by the UISearchController is set to false, that is, UISearchController.searchBar.showsScopeBar = false

in Swift.

The problem is that Apple's intent is to use a property showsScopeBar

that will be used with standalone search markers rather than search bars controlled by UISearchController. Unfortunately this is not called in the class documentation. Bad design decision in my opinion, but that's what it is.



The discussion is in the following thread https://devforums.apple.com/thread/235803 (dead link from 2018-01-26).

Good luck.

+2


source


This appears to be a known defect. There are several radr entries such as http://www.openradar.me/20702394 that link to similar problems and workarounds using sizeToFit ()

The workaround they suggest works, but only when applied in the viewDidLayoutSubviews. those. after all views have been posted.



override func viewDidLayoutSubviews() {
    self.searchController.searchBar.sizeToFit()
}

      

+1


source


Update for swift 3:

searchController.searchBar.showsScopeBar = false

      

Update for swift 4:

It seems that in swift 4 and ios 11 the search bar has changed. With the method shown above, the scope will in some cases be inside the search bar.

if #available(iOS 11.0, *) {
        navigationItem.searchController = searchController
        navigationItem.hidesSearchBarWhenScrolling = false
    } else {
        tableView.tableHeaderView = searchController.searchBar
    }

      

This fixed it for me. I am using the form shown above if ios 10 is available. If ios 11 is available, I change the technique and set the navigation view controller to the search controller. You will notice that it looks exactly the same as on ios 10

0


source







All Articles