Skip to content

Instantly share code, notes, and snippets.

@felipeslongo
Last active April 25, 2019 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipeslongo/925bf96b877b05bbd4fa41364d952432 to your computer and use it in GitHub Desktop.
Save felipeslongo/925bf96b877b05bbd4fa41364d952432 to your computer and use it in GitHub Desktop.
Adiciona barra de busca em qualquer versão do IOS
using System;
using App.iOS.Utils;
using UIKit;
namespace App.iOS.Extensions
{
//https://stackoverflow.com/questions/48007690/issue-with-uisearchcontroller-in-uitableview-on-ios-11
//https://stackoverflow.com/questions/28326269/uisearchbar-presented-by-uisearchcontroller-in-table-header-view-animates-too-fa?rq=1
public static class UIViewController_AdicionarBarraDeBusca
{
public static UISearchController AdicionarBarraDeBusca(this UIViewController @this)
{
if (VersaoIosSuportaBarraDeBuscaNoNavigationItem)
return AdicionarBarraDeBuscaAoNavigationItem(@this);
return AdicionarBarraDeBuscaDoJeitoObsoleto(@this);
}
private static bool VersaoIosSuportaBarraDeBuscaNoNavigationItem => UIDevice.CurrentDevice.CheckSystemVersion(11, 0);
private static UISearchController AdicionarBarraDeBuscaAoNavigationItem(UIViewController @this)
{
var searchController = new UISearchController(null as UIViewController);
searchController.SearchResultsUpdater = @this as IUISearchResultsUpdating ??
throw new ControllerNaoImplementaBarraDeBuscaException(@this);
searchController.ObscuresBackgroundDuringPresentation = false;
searchController.SearchBar.Placeholder = "Buscar";
@this.NavigationItem.SearchController = searchController;
@this.NavigationItem.HidesSearchBarWhenScrolling = false;
@this.DefinesPresentationContext = true;
return searchController;
}
private static UISearchController AdicionarBarraDeBuscaDoJeitoObsoleto(UIViewController @this)
{
var searchController = new UISearchController(null as UIViewController);
searchController.SearchResultsUpdater = @this as IUISearchResultsUpdating ??
throw new ControllerNaoImplementaBarraDeBuscaException(@this);
searchController.HidesNavigationBarDuringPresentation = true;
searchController.DimsBackgroundDuringPresentation = false;
searchController.SearchBar.Placeholder = "Buscar";
if (@this is UITableViewController tableView)
AdicionaBarraDeBuscaAoTableHeaderView(tableView, searchController);
else
AdicionaBarraDeBuscaAoTitleView(@this, searchController);
@this.DefinesPresentationContext = true;
@this.ExtendedLayoutIncludesOpaqueBars = true;
return searchController;
}
private static void AdicionaBarraDeBuscaAoTitleView(UIViewController @this, UISearchController searchController) => @this.NavigationItem.TitleView = searchController.SearchBar;
private static void AdicionaBarraDeBuscaAoTableHeaderView(UITableViewController tableView,
UISearchController searchController) =>
tableView.TableView.AtribuirTableHeaderView(searchController.SearchBar);
private class ControllerNaoImplementaBarraDeBuscaException : Exception
{
public ControllerNaoImplementaBarraDeBuscaException(UIViewController controller) :
base ($"Controller {controller.GetType().Name} deve implementar interface {nameof(IUISearchResultsUpdating)}")
{}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment