Xamarin 中的搜索栏Search Bars in Xamarin.iOS

UISearchBar 用于在值列表中进行搜索。The UISearchBar is used to search through a list of values.

它包含三个主要组件:It contains three main components:

  • 用于输入文本的字段。A field used to enter text. 用户可以利用它来输入搜索词。Users can utilize this to enter their search term.
  • 一个 "清除" 按钮,用于删除搜索字段中的任何文本。A clear button, to remove any text from the search field.
  • "取消" 按钮,用于退出搜索功能。A Cancel button, to exit the search function.


若要通过实例化新的搜索栏开始,请执行以下操作:To implement the search bar start by instantiating a new one:

searchBar = new UISearchBar();

然后将其放置。And then place it. 下面的示例演示如何将其放在导航栏或表的 HeaderView 中:The example below shows how to place it in the navigation bar or in the HeaderView of a Table:

NavigationItem.TitleView = searchBar;

// or

TableView.TableHeaderView = searchBar;

在搜索栏上设置属性:Setting properties on the Search bar:

 searchBar = new UISearchBar(){
                Placeholder = "Enter your search Item",
                Prompt = "Search Entered here",
                ShowsScopeBar = true,
                ScopeButtonTitles = new string[]{ "Boston", "London", "SF" },


按下 "搜索" 按钮时,引发 SearchButtonClicked 事件。Raise the SearchButtonClicked event when the search button is pressed. 这会调用搜索逻辑:This will call your search logic:

searchBar.SearchButtonClicked += (sender, e) => {
                Search ();

有关管理搜索栏和搜索结果的显示方式的信息,请参阅搜索控制器食谱。For information on managing the presentation of the search bar and search results, refer to the Search Controller recipe.

在设计器中使用搜索栏Using the Search Bar in the Designer

设计器提供了两个用于在设计器中实现搜索栏的选项The Designer offers two options for implementing a Search Bar in the designer

  • 搜索栏Search Bar
  • 搜索显示控制器的搜索栏(不推荐使用)Search Bar with Search Display Controller (Deprecated)


使用 "属性" 面板在搜索栏上设置属性Use the Property Panel to set properties on the search bar


下面对这些属性进行了说明:These properties are explained below:

  • 文本、占位符、提示–这些属性用于建议和指示用户应如何使用搜索栏。Text, Placeholder, Prompt – These properties are used to suggest and instruct how users should use the search bar. 例如,如果应用显示商店列表,则可以使用 prompt 属性来告知用户可以 "输入城市、故事名称或邮政编码"For example, if your app displayed a list of stores you could use the prompt property to advise that users can "Enter a city, story name, or Zip Code"

  • 搜索样式–可以将搜索栏设置为突出最小Search Style – You can set the search bar to either be Prominent or Minimal. 使用突出显示的内容将以淡色显示屏幕上的所有其他内容,搜索栏除外,导致将焦点绘制到搜索栏。Using the prominent will tint everything else on screen, except for the search bar, causing the focus to be drawn to the search bar. 最小样式搜索栏会与其周围的混合。The minimal style search bar will blend in with its surroundings.

  • 功能–启用这些属性仅显示 UI 元素。Capabilities – Enabling these properties only displays the UI element. 必须通过按搜索栏 API 文档中的详细说明引发正确事件来实现这些功能。The functionality must be implemented for these by raising the correct event as detailed in the Search Bar API docs

    • 显示搜索结果/书签按钮–在搜索栏上显示搜索结果或书签图标Shows Search Results / Bookmarks button – Shows a Search Results or Bookmarks icon on the search bar
    • 显示 "取消" 按钮–允许用户退出搜索功能。Shows Cancel button – Allows users to exit out of the search function. 建议选择此选项。It is recommended that this is selected.
    • 显示范围栏–这允许用户限制其搜索范围。Shows Scope Bar – This allows users to limit the scope of their search. 例如,当你在音乐应用程序中搜索时,用户可以选择是否要搜索特定歌曲或艺术家 Apple Music 或其库。For example, when searching in the music app the user can select whether they want to search Apple Music or their Library for a particular song or artist. 若要显示各种选项,请将标题数组添加到ScopeBarTitles属性。To display various options, add an array of titles to the ScopeBarTitles property. 搜索栏范围标题Search bar scope titles
  • 文本行为-这些选项用于处理键入时用户输入的格式。Text behavior – These options are used to address how the user input is formatted when they are typing. 大小写将设置每个单词或句子的开头,或将每个字符设置为大写。Capitalization will set the start of each word or sentence, or every character as upper case. 更正和拼写检查提示用户键入时,会提示用户输入拼写建议。Correction and Spell Checking with prompt the user with suggested spellings of words as they type.

  • 键盘–控制为输入显示的键盘样式,并因此可以在键盘上使用哪些键。Keyboard – Controls the keyboard style displayed for the input, and therefore what keys are available on the keyboard. 这包括数字板、手机 Pad、电子邮件和其他选项。This includes Number Pad, Phone Pad, Email, URL along with other options.

  • 外观–控制键盘的外观样式,并将为深色或浅色主题。Appearance – Controls the appearance style of the keyboard and will be either dark or light themed.

  • 返回键–更改返回键上的标签,以更好地反映要采取的操作。Return Key – Change the label on the Return key to better reflect what action will be taken. 受支持的值包括 "开始"、"联接"、"下一步"、"路由"Supported values include Go, Join, Next, Route, Done, and Search.

  • Secure –标识是否屏蔽输入(例如密码输入)。Secure – Identifies whether the input is masked (such as for a Password input).