Search Bars

Search Bars
A search bar provides an interface for text-based searches with a text box and buttons such as search and cancel. A search bar accepts text from users, which can be used as input for a search (shown here with placeholder text). A scope bar, which is available only in conjunction with a search bar—allows users to define the scope of a search (shown here below a search bar).

image: ../Art/uisearchbar_intro_2x.png
image: ../Art/uisearchbar_intro_2x.png

Purpose. Search bars allow users to:
Quickly find a value in a large collection

Create a scope filter

Implementation. Search bars are implemented in the UISearchBar
class and discussed in the UISearchBar Class Reference.
Configuration. Configure search bars in Interface Builder, in the Search Bar section of the Attributes Inspector. A few configurations cannot be made through the Attributes Inspector, so you must make them programmatically. You can set other configurations programmatically, too, if you prefer.

image: ../Art/uisearchbar_attributes_inspector_plain_2x.png
image: ../Art/uisearchbar_attributes_inspector_plain_2x.png

Content of Search Bars
The Text (text
) field contains the current search text; you can use it to set an initial search. Don’t use it to provide a description of the search; use placeholder text instead. Placeholder text is specified in the Placeholder (placeholder
) field, and is visible only when there is no other text in the search field. Placeholder text is styled differently to communicate its different meaning to the user and it is automatically cleared when the user starts typing. It is suitable for very short descriptions of what the user should enter in the search field.

image: ../Art/uisearchbar_attributes_inspector_group-searchterm_captions.png
image: ../Art/uisearchbar_attributes_inspector_group-searchterm_captions.png

The prompt text is specified in the Prompt (prompt
) field. It appears directly above the search bar. Unlike the placeholder text, the prompt text is visible whether or not the user has entered text in the search field, so it is suitable for longer descriptions or directions.
image: ../Art/uisearchbar_attributes_inspector_group-capabilities.png
image: ../Art/uisearchbar_attributes_inspector_group-capabilities.png

Search bars can display a number of different buttons. The Cancel button is intended to terminate a search operation; you can display this button by selecting the Shows Cancel Button checkbox. The Search Results and Bookmarks buttons appear in the right side of search bar, and can be toggled to display those respective views. You can display one of these buttons by selecting either the Shows Search Results Button (showsSearchResultsButton
) or Shows Bookmarks Button (showsBookmarkButton
) checkbox. Note that you cannot display both of these buttons simultaneously; if both properties are enabled, only the Search Results button is visible.
NOTE
These buttons are merely user interface elements and have no functionality. You must implement the appropriate functionality yourself using the corresponding UISearchBarDelegate
methods.

image: ../Art/uisearchbar_scope.png
image: ../Art/uisearchbar_scope.png

A search bar can also display a scope bar, which lets users limit the scope of a search. For example, when searching in an email app, the user could restrict the search to the Inbox or to a particular folder. To display a scope bar, check the Shows Scope Bar (showsScopeBar
) box and add an array of scope bar titles as strings to the Scope Titles (scopeButtonTitles
) field.

image: ../Art/uisearchbar_attributes_inspector_group-scope_titles.png
image: ../Art/uisearchbar_attributes_inspector_group-scope_titles.png

Behavior of Search Bars
Search bars need a delegate to handle user interaction. You implement the UISearchBarDelegate
protocol on a delegate object to respond to user actions—for example, performing the search. Every search bar needs a delegate object that implements the UISearchBarDelegate
protocol. The delegate is responsible for taking actions in response to user input such as editing the search text, starting or canceling a search, and tapping in the scope bar. At the very minimum, the delegate needs to perform a search after text is entered in the text field.
A user types content into a search bar using a keyboard, which has a number of customization options:

image: ../Art/uisearchbar_attributes_inspector_group-textinput.png
image: ../Art/uisearchbar_attributes_inspector_group-textinput.png

Keyboard layout. The Keyboard field allows you to select from a number of different keyboard layouts. Match the keyboard layout to the purpose and scope of the search bar. The default keyboard layout is an alphanumeric keyboard in the device’s default language. For a list of possible keyboard types, see UIKeyboardType
.

Capitalization scheme. The Capitalization field specifies how text should be capitalized in the search bar: no capitalization, every word, every sentence, or every character. The no capitalization scheme is selected by default.

Auto-correction. The Correction field simply disables or enables auto-correct in the search bar.

Appearance of Search Bars
You can customize the appearance of a search bar by setting the following properties:

image: ../Art/uisearchbar_callouts.png
image: ../Art/uisearchbar_callouts.png

To customize the appearance of all search bars in your app, the appearance proxy (for example, [UISearchBar appearance]
). For more information about appearance proxies, see Appearance Proxies.
Style
Bars have two standard appearance styles: translucent white with dark text (default) or translucent black with light text. Use the Style (barStyle
) field to select one of these standard styles.
image: ../Art/uisearchbar_attributes_inspector_group-appearance.png
image: ../Art/uisearchbar_attributes_inspector_group-appearance.png

Tint Color
You can specify a custom tint color for the search bar background using the Tint (barTintColor
) field. The default background tint color is white.

image: ../Art/uisearchbar_attributes_inspector_group-appearance.png
image: ../Art/uisearchbar_attributes_inspector_group-appearance.png

Additionally, you can set a custom tint color for the interactive elements within a search bar—including the scope bar, cancel button, and cursor—programmatically using the tintColor
property. The search bar will inherit its superview’s tint color if a custom one is set, or show the default system blue color if none is set. For more information, see Tint Color.

Background Images
A search bar can have a background image that covers the area behind the search field. Use thebackgroundImage
property to set a background image for your search bar. You can also set the background image for a search bar’s scope bar using the scopeBarBackgroundImage
property. Single-pixel images or stretchable images are stretched; otherwise, the image is tiled. If you set one of these background image properties, you should also set the other to give your app interface a consistent look.

Translucency
Search bars are translucent by default on iOS 7. Additionally, there is a system blur applied to all search bars. This allows your content to show through underneath the bar.
These settings automatically apply when you set any style for barStyle
or any custom color for barTintColor
. If you prefer, you can make the search bar opaque by setting the translucent
property to NO
programmatically. In this case, the bar draws an opaque background using black if the search bar has UIBarStyleBlack
style, white if the search bar has UIBarStyleDefault
, or the search bar’s barTintColor
if a custom value is defined.
If the search bar has a custom background image, the default translucency is automatically inferred from the average alpha values of the image. If the average alpha is less than 1.0, the search bar will be translucent by default; if the average alpha is 1.0, the search bar will be opaque by default. If you set the translucent
property to YES
on a search bar with an opaque custom background image, the search bar makes the image translucent. If you set the translucent
property to NO
on a search bar with a translucent custom background image, the search bar provides an opaque background for the image using black if the search bar hasUIBarStyleBlack
style, white if the search bar has UIBarStyleDefault
, or the search bar’s barTintColor
if a custom value is defined.

Layout
You can also control certain aspects of the search bar’s layout by providing position adjustments: for icons using the positionAdjustmentForSearchBarIcon:
method, for the background image using thesearchFieldBackgroundPositionAdjustment
property, and for search text using the searchTextPositionAdjustment
property.

Using Auto Layout with Search Bars
You can create Auto Layout constraints between a search bar and other user interface elements. You can create any type of constraint for a search bar besides a baseline constraint.
For general information about using Auto Layout with iOS views, see Using Auto Layout with Views.

Making Search Bars Accessible
Search bars are accessible by default.
For general information about making iOS views accessible, see Making Views Accessible.

Internationalizing Search Bars
To internationalize a search bar, you must provide localized strings for the following properties:
placeholder

prompt

text

scopeButtonTitles

For more information, see Internationalization and Localization Guide.

Debugging Navigation Bars
When debugging issues with navigation bars, watch for these common pitfalls:
Specifying conflicting appearance settings. When customizing search bar appearance with a style or color, use one option or the other, but not both. Conflicting settings for search bar appearance will be resolved in favor of the most recently set value. For example, setting a new style clears any custom tint color you have set. Similarly, setting a custom tint color overrides any style you have set.

Performance issues. If search operations can be carried out very rapidly, it is possible to update the search results as the user is typing by implementing the searchBar:textDidChange:
method on the delegate object. However, if a search operation takes more time, you should wait until the user taps the Search button before beginning the search in the searchBarSearchButtonClicked:
method. Always perform search operations a background thread to avoid blocking the main thread. This keeps your app responsive to the user while the search is running and provides a better user experience.

Elements Similar to a Search Bar
The following element provides similar functionality to a search bar:
Toolbar. A toolbar object contains controls that allow the user to perform actions related to objects onscreen. For more information, see Toolbars.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容