How to add SearchBar and NavigationPage.PrefersLargeTitles IOS using xaml?

timarshatimarsha Member ✭✭

Hello! How to add SearchBar and NavigationPage.PrefersLargeTitles IOS using xaml?

Best Answer

Answers

  • NMackayNMackay GBInsider, University mod

    @timarsha said:
    Hello! How to add SearchBar and NavigationPage.PrefersLargeTitles IOS using xaml?

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/platform-specifics/consuming/ios#displaying-large-titles

    You can add you can add your own searchbar to the Title using the TitleView feature (Forms 3.2 upwards...recommend 3.4 SR1)

    https://blog.xamarin.com/xamarin-forms-3-2-0-released-going-big-with-little-things/

  • timarshatimarsha Member ✭✭

    I added SearchBar, but it is located above leargeTitle

    <NavigationPage.TitleView>

    </NavigationPage.TitleView>

  • timarshatimarsha Member ✭✭

    Thanks

  • BrandonMinnickBrandonMinnick USXamarin Team Xamurai
    edited August 10

    There's currently no way to do it in XAML.

    Here’s a blog post I did that shows how to do it for Xamarin.Forms and it covers both iOS and Android: https://www.codetraveler.io/2019/08/10/adding-a-search-bar-to-xamarin-forms-navigationpage/

    And here’s a sample app showing the completed code: https://github.com/brminnick/GitHubXamarin

    To answer your question, here’s how to do it for Xamarin.iOS.

    App.cs

    Use a Xamarin.Forms Platform-Specific to use LargeTitles

    using Xamarin.Forms.PlatformConfiguration;
    using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
    
    public class App : Xamarin.Forms.Application
    {
        public App()
        {
            var navigationPage = new Xamarin.Forms.NavigationPage(new MyContentPage());
    
            navigationPage.On<iOS>().SetPrefersLargeTitles(true);
    
            MainPage = navigationPage;
        }
    }
    

    Xamarin.Forms Page

    public class MyContentPage : ContentPage, ISearchPage,
    {
        public MyContentPage()
        {
            SearchBarTextChanged += HandleSearchBarTextChanged
        }
    
        public event EventHandler<string> SearchBarTextChanged;
    
        public void OnSearchBarTextChanged(in string text) => SearchBarTextChanged?.Invoke(this, text);
        void HandleSearchBarTextChanged(object sender, string searchBarText)
        {
            //Logic to handle updated search bar text
        }     
    }
    

    ISearchPage Interface

    public interface ISearchPage
    {
        void OnSearchBarTextChanged(in string text);
        event EventHandler<string> SearchBarTextChanged;
    }
    

    iOS Custom Renderer

    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using MyNamespace;
    using MyNamespace.iOS;
    using UIKit;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    
    [assembly: ExportRenderer(typeof(MyContentPage), typeof(SearchPageRenderer))]
    namespace MyNamespace.iOS
    {
        public class SearchPageRenderer : PageRenderer, IUISearchResultsUpdating
        {
            bool _isFirstAppearing = true;
    
            public override void WillMoveToParentViewController(UIViewController parent)
            {
                base.WillMoveToParentViewController(parent);
    
                var searchController = new UISearchController(searchResultsController: null)
                {
                    SearchResultsUpdater = this,
                    DimsBackgroundDuringPresentation = false,
                    HidesNavigationBarDuringPresentation = false,
                    HidesBottomBarWhenPushed = true
                };
                searchController.SearchBar.Placeholder = string.Empty;
    
                parent.NavigationItem.SearchController = searchController;
    
                DefinesPresentationContext = true;
            }
    
            public override void ViewDidAppear(bool animated)
            {
                base.ViewDidAppear(animated);
    
                //Work-around to ensure the SearchController appears when the page first appears https://stackoverflow.com/a/46313164/5953643
                if (_isFirstAppearing)
                {
                    ParentViewController.NavigationItem.SearchController.Active = true;
                    ParentViewController.NavigationItem.SearchController.Active = false;
    
                    _isFirstAppearing = false;
                }
            }
    
            public void UpdateSearchResultsForSearchController(UISearchController searchController)
            {
                if (Element is ISearchPage searchPage)
                    searchPage.OnSearchBarTextChanged(searchController.SearchBar.Text);
            }
        }
    }
    
Sign In or Register to comment.