{"id":35307,"date":"2018-02-12T15:00:25","date_gmt":"2018-02-12T23:00:25","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=35307"},"modified":"2019-04-04T08:37:03","modified_gmt":"2019-04-04T15:37:03","slug":"add-search-bar-xamarin-ios-11","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/add-search-bar-xamarin-ios-11\/","title":{"rendered":"Add a Search Bar to Your Xamarin Apps in iOS 11"},"content":{"rendered":"<p>\t\t\t\tAs described in the <a href=\"https:\/\/blog.xamarin.com\/go-large-ios-11\/\">Go Large with iOS 11<\/a> post, view controllers in iOS 11 can use large titles to improve the clarity of an app&#8217;s user interface. To complement a view controller&#8217;s large title, it&#8217;s also possible to add a search bar, which allows users to quickly find content without navigating to a new screen. Widely used in iOS applications, Apple&#8217;s <a href=\"https:\/\/developer.apple.com\/ios\/human-interface-guidelines\/bars\/search-bars\/\">Human Interface Guidelines<\/a> provide a good overview of search bars.<\/p>\n<h2>Implementing a Search Bar<\/h2>\n<p>Check out the <a href=\"https:\/\/github.com\/xamarin\/ios-samples\/tree\/master\/ios11\/LargeTitlesSample\">Large Titles Sample<\/a> to see an example of an iOS 11 view controller with a large title and a search bar. Start the app and swipe down to reveal the search bar:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/searchbar-uisearchcontroller.png\" alt=\"A search bar in iOS 11\" width=\"397\" height=\"320\" class=\"aligncenter size-full wp-image-35347\" \/><\/p>\n<p>Entering a term in the search bar initiates a search through the book list for relevant results. The various classes and methods that enable a search bar are described below.<\/p>\n<h3>Searchable View Controller<\/h3>\n<p>In this example, the searchable view controller is represented in code as the <code>LargeTitlesViewController<\/code> class, which is defined by <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/partial-classes-and-methods\">partial classes<\/a> in two files: <a href=\"https:\/\/github.com\/xamarin\/ios-samples\/blob\/master\/ios11\/LargeTitlesSample\/LargeTitles\/LargeTitlesViewController.cs\"><strong>LargeTitlesViewController.cs<\/strong><\/a> and <a href=\"https:\/\/github.com\/xamarin\/ios-samples\/blob\/master\/ios11\/LargeTitlesSample\/LargeTitles\/LargeTitlesViewController%2BSearch.cs\"><strong>LargeTitlesViewController+Search.cs<\/strong><\/a>.<\/p>\n<h3>UISearchController<\/h3>\n<p>iOS uses the <a href=\"https:\/\/developer.xamarin.com\/api\/type\/UIKit.UISearchController\/\"><code>UISearchController<\/code><\/a> class to tie together the various bits that must cooperate to enable a search bar. <code>UISearchController<\/code> creates the search bar UI and automatically calls the method to run a search for a user&#8217;s query.<\/p>\n<p>In <strong>LargeTitlesViewController+Search.cs<\/strong>, the <code>ViewDidLoad<\/code> method instantiates the <code>UISearchController<\/code>:<\/p>\n<pre><code>\npublic override void ViewDidLoad()\n{\n    \/\/ ...\n    var search = new UISearchController(searchResultsController: null)\n    {\n        DimsBackgroundDuringPresentation = true\n    };\n    \/\/ ...\n}\n<\/code><\/pre>\n<p>Setting <a href=\"https:\/\/developer.xamarin.com\/api\/property\/UIKit.UISearchController.DimsBackgroundDuringPresentation\/\"><code>DimsBackgroundDuringPresentation<\/code><\/a> to <code>true<\/code> dims the background of the primary view controller while a user interacts with the search bar. This screenshot shows the difference between a dimmed and a normal background:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/searchbar-dimming_comparison.png\" alt=\"Dimming with a search bar\" width=\"781\" height=\"320\" class=\"aligncenter size-full wp-image-35354\" \/><\/p>\n<h3>Search Results View Controller<\/h3>\n<p>Search results can be visualized in many ways. Some data should be shown as a list, while other data might best be represented on a map. You can pass a view controller for displaying search results to the <code>searchResultsController<\/code> parameter of the <a href=\"https:\/\/developer.xamarin.com\/api\/constructor\/UIKit.UISearchController.UISearchController\/p\/UIKit.UIViewController\/\"><code>UISearchController<\/code> constructor<\/a> or set this value to <code>null<\/code> to indicate that search results should be displayed using the searchable view controller itself.<\/p>\n<h3>Search Results Updater<\/h3>\n<p>When the <code>UISearchController<\/code> needs to kick off a search for a user&#8217;s query, it calls the <a href=\"https:\/\/developer.xamarin.com\/api\/member\/UIKit.IUISearchResultsUpdating.UpdateSearchResultsForSearchController\/\"><code>UpdateSearchResultsForSearchController<\/code><\/a> method on its <a href=\"https:\/\/developer.xamarin.com\/api\/property\/UIKit.UISearchController.SearchResultsUpdater\/\"><code>SearchResultsUpdater<\/code><\/a>. This property implements the <a href=\"https:\/\/developer.xamarin.com\/api\/type\/UIKit.IUISearchResultsUpdating\/\"><code>IUISearchResultsUpdating<\/code><\/a> interface, which has only one method:<\/p>\n<pre><code>\nvoid UpdateSearchResultsForSearchController (UISearchController searchController);\n<\/code><\/pre>\n<p><code>LargeTitlesViewController<\/code> implements this interface, so it sets itself as the <code>UISearchController<\/code>&#8216;s <code>SearchResultsUpdater<\/code> and implements the required method:<\/p>\n<pre><code>\npublic override void ViewDidLoad()\n{\n    \/\/ ...\n    search.SearchResultsUpdater = this;\n    \/\/ ...\n}\n\npublic void UpdateSearchResultsForSearchController(UISearchController searchController)\n{\n    var find = searchController.SearchBar.Text;\n    if (!String.IsNullOrEmpty(find))\n    {\n        searchResults = titles.Where(t =&gt; t.ToLower().Contains(find.ToLower())).Select(p =&gt; p).ToArray();\n    }\n    else\n    {\n        searchResults = null;\n    }\n    TableView.ReloadData();\n}\n<\/code><\/pre>\n<p>This search implementation uses <a href=\"https:\/\/docs.microsoft.com\/dotnet\/csharp\/programming-guide\/concepts\/linq\/getting-started-with-linq\">LINQ<\/a> to search the list of titles, saves the results in the <code>searchResults<\/code> field, and reloads the table view&#8217;s data. When this happens, the table view will display either the search results or the entire book list.<\/p>\n<h3>NavigationItem<\/h3>\n<p>Prior versions of iOS required developers to manually position the <code>UISearchController<\/code>&#8216;s search bar in their application&#8217;s interface. In iOS 11, setting the <a href=\"https:\/\/developer.xamarin.com\/api\/property\/UIKit.UINavigationItem.SearchController\/\"><code>SearchController<\/code><\/a> property on a view controller&#8217;s <a href=\"https:\/\/developer.xamarin.com\/api\/property\/UIKit.UIViewController.NavigationItem\/\"><code>NavigationItem<\/code><\/a> places a search bar in the navigation bar. For example, in <strong>LargeTitlesViewController+Search.cs<\/strong>:<\/p>\n<pre><code>\npublic override void ViewDidLoad()\n{\n    \/\/ ...\n    NavigationItem.SearchController = search;\n    \/\/ ...\n}\n<\/code><\/pre>\n<p>Since <code>SearchController<\/code> is set on <code>NavigationItem<\/code>, the search bar will only appear if your searchable view controller is contained by a navigation controller. In the <em>Large Titles<\/em> sample app, <strong>Main.storyboard<\/strong> wraps <code>LargeTitlesViewController<\/code> in a navigation controller.<\/p>\n<h2>Other Useful Properties<\/h2>\n<p>There are a number of other properties that can help improve the in app search experience for your users. Below are a few of our favorites:<\/p>\n<h3>UISearchController<\/h3>\n<ul>\n<li><a href=\"https:\/\/developer.xamarin.com\/api\/property\/UIKit.UISearchController.HidesNavigationBarDuringPresentation\/\"><code>HidesNavigationBarDuringPresentation<\/code><\/a> hides the navigation bar during a search. Defaults to <code>true<\/code>.<\/li>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/searchbar-navbar_comparison.png\" alt=\"Hiding the navigation bar during a search\" width=\"781\" height=\"320\" class=\"aligncenter size-full wp-image-35358\" \/>\n<\/ul>\n<h3>UINavigationItem<\/h3>\n<ul>\n<li><a href=\"https:\/\/developer.xamarin.com\/api\/property\/UIKit.UINavigationItem.HidesSearchBarWhenScrolling\/\"><code>HidesSearchBarWhenScrolling<\/code><\/a> hides the search bar when a user scrolls the searchable view controller. It defaults to <code>true<\/code>.<\/li>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/searchbar-scrolling_comparison.png\" alt=\"Hiding the search bar when scrolling\" width=\"781\" height=\"320\" class=\"aligncenter size-full wp-image-35360\" \/>\n<\/ul>\n<h3>UISearchController&#8217;s SearchBar<\/h3>\n<ul>\n<li><a href=\"https:\/\/developer.xamarin.com\/api\/property\/UIKit.UISearchBar.Placeholder\/\"><code>Placeholder<\/code><\/a> contains hint text that is displayed when there is no other text in the search bar.<\/li>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/searchbar-placeholder.png\" alt=\"Placeholder text in a search bar\" width=\"397\" height=\"320\" class=\"aligncenter size-full wp-image-35359\" \/>\n<\/ul>\n<h2>Wrapping Up<\/h2>\n<p>To complement your <a href=\"https:\/\/blog.xamarin.com\/go-large-ios-11\/\">view controller&#8217;s large titles<\/a>, add search bars that will improve the clarity of your app&#8217;s user interface. Download the <a href=\"https:\/\/github.com\/xamarin\/ios-samples\/tree\/master\/ios11\/LargeTitlesSample\">LargeTitles sample from GitHub<\/a>, and check out the <a href=\"https:\/\/developer.xamarin.com\/guides\/ios\/platform_features\/introduction-to-ios11\/\">Xamarin Developer Center<\/a> for more information about iOS 11 features.<\/p>\n<p><a href=\"https:\/\/forums.xamarin.com\/122381\/\">Discuss this post on the forums!<\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As described in the Go Large with iOS 11 post, view controllers in iOS 11 can use large titles to improve the clarity of an app&#8217;s user interface. To complement a view controller&#8217;s large title, it&#8217;s also possible to add a search bar, which allows users to quickly find content without navigating to a new [&hellip;]<\/p>\n","protected":false},"author":567,"featured_media":35359,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[6,4],"class_list":["post-35307","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-ios","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>As described in the Go Large with iOS 11 post, view controllers in iOS 11 can use large titles to improve the clarity of an app&#8217;s user interface. To complement a view controller&#8217;s large title, it&#8217;s also possible to add a search bar, which allows users to quickly find content without navigating to a new [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/35307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/567"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=35307"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/35307\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=35307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=35307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=35307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}