IOS Search bar has changed a bit from IOS8. Have a new way of using it.
The Search bar for iOS works like this:
When the user points to search bar, it gets the input focus and a new page (Viewcontroller) appears, which is the page that displays the search results. This page displays the search results when the user enters it in search bar. The general result is displayed in the table view, so there is usually a table view on this search result page.
Now, in addition to search bar,
There's one more option: Search controller (Uisearchcontroller)
Use the search bar to follow these steps:
1 Create or drag a search bar control in Interface Builder
2 You need to manually respond to the search bar's get focus event, create a results page, and display the search results page.
3 responds to text change events in the text box of the search bar, followed by a list of results from the current text, displayed in the table view.
The search controller contains the searches bar and the results page. The search results page is automatically displayed when the page is needed, and search controllers are more convenient than using search bar alone.
With the search controller, there is no small difference between dragging a control and creating it manually. When you drag a search controller in the interface builder, it automatically takes a page of results, and when you manually create it, you manually specify the search results page.
This search bar simply drags a search controller to the head of a table view.
You can see that the search bar of the search controller is embedded in the table view.
When the search bar gets the input focus, it looks like this:
Search bar automatically tops the navigation bar by default and takes its place, of course, which allows you to set the properties of the search bar to prevent it from doing so. When the search bar gets the input focus, a translucent page appears, and the main interface is semi-gray. When you enter a character, the search results page appears as follows:
Of course, whatever you enter does not show any results. Because the results are entirely in your own control. You need to respond to the text change event in Search bar, where you can calculate a list of search results and display the list in table view so that the results are visible to the user.
When you see the drag-in search bar in the interface designer, you also add a view controller:
You can set properties for this view controller separately.
Manually create the search controller to invoke the initialization method of Uisearchcontroller-
-(Instancetype) Initwithsearchresultscontroller: (Nullable Uiviewcontroller *) Searchresultscontroller;
The parameter Searchresultscontroller is the page that displays the results. Unlike drag controls, you must pass a result page to it. Then we can design the results page ourselves, but most of them use TableView to display the results, so it is enough to use only one table view controller.
Many apps are designed for search pages:
There is a search button on the navigation bar on the main page
Page 1
When you click on another page, you will see the Search bar on this page.
Page 2
And there may be some out-of-the-box keywords, all in the button, click to search those keywords directly. The results page is displayed when you enter it in Search bar.
But if you use the Search bar controller and want to display the search bar in the navigation bar, it seems impossible. The behavior of the Search bar controller becomes very strange. The search bar will be used alone.
This means that the search bar in page 2 above is a separate search bar. Its behavior is our own realization. We respond to its get input focus event, where the results page is displayed. The resulting page is usually displayed below the navigation bar, so the resulting page should not be a view controller, but directly with a table view. Calculate the appropriate location for this table view and display it.
The way to respond to events in Search bar is, of course, with protocols and proxies. There are many methods in the proxy uisearchbardelegate of search bar. Most are used to inform the agent what has happened. Agent Implementation Method
-(BOOL) searchbarshouldbeginediting: (Uisearchbar *) Searchbar
Can respond to open focus events such as:
-
(BOOL) Searchbarshouldbeginediting: (Uisearchbar *) searchbar{
Display the results under the Navigation Bar table view, should use AutoLayout, here is easy to reduce the bottom of the navigation bar directly the Y value
Self.resulttv.frame=cgrectmake (0, Self.view.bounds.size.width, self.view.bounds.size.height-64);
return YES;
}
Resulttv is a table view that was created in advance.
There are many other methods that allow us to monitor changes in user input at any time, and we can calculate new results and display them in table view at each change.
IOS9 Uisearchbar Detailed