Use the search function to the project, before they are used UISearchBarUISearchDisplayController or if you need highly customized search box should be completed by UITextField plus the click event. Because want to be lazy, I will use the new UISearchController iOS 8 after the launch of the project, after all we just down the adaptation to 8, in order to avoid the waste API problems in the future, I will use the UISearchController directly, and found all kinds of pits.
1 I first paste the screenshot in question
In the process I use UISearchController, met a wonderful question, I show the interface and search interface is the same interface, is under the code
UISearchController _searchController = [[UISearchController alloc] / / create initWithSearchResultsController:nil];
I’m not alone to create a UITableViewContrller to display data source content search, click on this searchBar after, UISearchBar will automatically take the navigation bar (in fact is an attribute of hidesNavigationBarDuringPresentation), set to YES, click searchBar will automatically hide the navigation bar (see below), I suggest that this property is set to true, otherwise it will cause the UISearchBar to sink 44 at the height of the navigation bar under. The reasons for this problem will be mentioned below.
The problem I have is shown in the screenshot below
SearchBar per click left on the first interface, then click the Cancel button to return to the display interface, but the UISearchBar will go on migration, such as right on the interface shown, I really Speechless, but this problem only at iOS below 9, above 9 is good. The navigation bar will block the UISearchBar, and each return will be offset up to 20.
The solutions found on the Internet are the following:
In 1 – (UISearchController *) searchController method with self.definesPresentationContext = YES; this line of code
2 in the right place to add [self.searchController.searchBar sizeToFit]; 3
in the viewDidDisappear method to add self.navigationBar.translucent = NO; this line of code
Scenario 1, set definesPresentationContext to true, can ensure that the user will not be able to remain in the UISearchController bar in the activated state push to the next view search after the interface. But I’ve explained that my display interface and search interface are the same controller. So this solution doesn’t solve my problem. Apple
official explanation of it is know where you want UISearchController to / be displayed
a, if you do not add this line of code in the setting of hidesNavigationBarDuringPresentation, this property is YES, the search box to enter edit mode will lead to searchBar, not visible, offset -64;
in the NO setting, enter edit mode the input will lead to a height of 64 IOUs, speculation is not rendered
B navigation bar, if you add this line of code in the setting of hidesNavigationBarDuringPresentation, this property is YES, the input box to enter edit mode normal display and use; in the set to NO when the search box enter edit mode lead to a downward shift of 64 specific reasons, temporarily not found when you do not have to set the sear scheme of 2: ChBar frame, this line of code can help you adjust the location of the searchBar. Just auxiliary role, in fact, the default height of UISearchBar is 44, the program 3:translucent attribute is set or not, and the location of the self.view Frame. When translucent is set to NO, the location of the self.view starts at the bottom of the navbar. Vice versa。 It doesn’t solve my problem
2 the following is an example of what I can use on the Internet
1 [self shuffledAlphabet] is a method to generate three random letters, including the need to follow the four agent method, including UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating. Because UISearchController is used in conjunction with UITableView.
2.[self shuffledAlphabet] method screenshot
3 follow the UITableViewDataSource protocol
Datalist is the original data, searchList is the content of the search data.
4 according to the method of UISearchResultsUpdating protocol, the method is to search the content of the method
screenshot 2016-09-12 11 when 04 minutes 44 seconds.Jpg
The blue box around the code, because of the need to modify the UISearchController UISearchBar I project the Cancel button in the color of the text, I use KVC to modify, and traverse the UISearchBar controls to modify the way. The default cancel button text is Cancel
5 follow the UISearchControllerDelegate protocol
is divided into five stages
The direct use of the example code above, you can solve my problems, using the above example code, must remember to add searchBar to searchController tableView tableHeaderView, before I was directly add searchBar to the self.view, and there is no set it frame.[self.view addSubview:_searchController.searchBar];
Let me the most depressing is that I wrote a demo test, no problem, but the use of sticking into my project, there will be a problem before, after I finally solved the problem of partial revision up I suspect it is ill, Xcode, but also will come out a new problem, as shown below
Search results, cell part will overlap with the searchBar, in order to solve this problem, I in the following two agent method, add a few lines of code
And be in – (void) updateSearchResultsForSearchController: (UISearchController *) searchController method added code self.edgesForExtendedLayout = UIRectEdgeNone; / / without words, UISearchBar returns after moving up
In the book, there is an article about this situation, the following is a link, you can refer to the following.
UISearchController summary of a bit chaotic, what do not understand you can leave a message to ask me, for a long time did not send a document, and will meet the problems encountered in the project.