brminnick/GitTrends

Use Safe Area in IOS not working with Page Search

lu-nguyen-khoa opened this issue · 2 comments

I have PageSeachRenderer in IOS Renderer same you. But it content not working inside SafeArea. How to fix that?
image

Thanks @haiduong741!

It looks like the original SearchPageRenderer didn't accommodate the UISearchController height.

The work-around is to add the UISearchController.SearchBar.Frame.Height to the ContentPage.Padding.Bottom

[assembly: ExportRenderer(typeof(RepositoryPage), typeof(SearchPageRenderer))]
namespace GitTrends.iOS
{
    public class SearchPageRenderer : PageRenderer, IUISearchResultsUpdating
    {
        // ...
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
                e.OldElement.SizeChanged -= HandleSizeChanged;

            if (e.NewElement != null)
                e.NewElement.SizeChanged += HandleSizeChanged;
        }

        //Work-around to accommodate UISearchController height, https://github.com/brminnick/GitTrends/issues/171
        void HandleSizeChanged(object sender, EventArgs e)
        {
            if (ParentViewController?.NavigationItem.SearchController != null
                && Element.Height > -1
                && Element is Page page)
            {
                Element.SizeChanged -= HandleSizeChanged;

                page.Padding = new Thickness(page.Padding.Left,
                                                page.Padding.Top,
                                                page.Padding.Right,
                                                page.Padding.Bottom + _searchController.SearchBar.Frame.Height);
            }
        }
        }
    }
}

I've also updated this blog post to include the work-around:

Adding a Search Bar to Xamarin.Forms.NavigationPage
https://codetraveler.io/2020/10/31/adding-a-search-bar-to-xamarin-forms-navigationpage/