UISearchBar

A search bar (UISearchBar) is essentially a variety of text field, though it is not in fact a UITextField subclass. It is displayed as a rounded rectangle containing a magnifying glass icon, where the user can enter text (Figure 25-5). It does not, of itself, do any searching or display the results of a search; a common interface involves displaying the results of a search as a table, and a controller class, UISearchDisplayController, makes this easy to do (see Chapter 21).

A search bar with a search results button

Figure 25-5. A search bar with a search results button

A search bar’s current text is its text property. It can have a placeholder, which appears when there is no text. A prompt can be displayed above the search bar to explain its purpose. Delegate methods (UISearchBarDelegate) notify you of editing events:

  • searchBarShouldBeginEditing:
  • searchBarTextDidBeginEditing:
  • searchBar:textDidChange:
  • searchBar:shouldChangeTextInRange:replacementText:
  • searchBarShouldEndEditing:
  • searchBarTextDidEndEditing:

A search bar has a barStyle, for which your choices are the same as for a toolbar or navigation bar: either UIBarStyleDefault or UIBarStyleBlack, and if the latter, either translucent or not. Alternatively, the search bar may have a tintColor; as with a toolbar, if this is set, the barStyle is ignored. (See Chapter 19 and later in this chapter.) Because of this, a search bar looks good at the top of the screen, ...

Get Programming iOS 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.