How can I style the Xamarin.Forms SearchBar in iOS? -
How can I style the Xamarin.Forms SearchBar in iOS? -
i'm trying style xamarin.forms searchbar , can see there backgroundcolor property, no matter set, property ignored in ios.
is possible customize xamarin.forms searchbar in ios (and how)?
as near can tell, xamarin.forms not property implement backgroundcolor property, or broken. uisearchbar property closest true background color bartint, not set xforms.
to solve this, took comments heart , created own custom searchbar custom renderer in order extend bartint property few other things wanted.
note: in order utilize custom renderers create sure updated xamarin.forms 1.1.1.6206 or greater. update platform version utilize nuget in visual studio, or built in bundle manager in xamarinstudio.
you need 2 classes, first 1 customsearchbar, ui utilize hold custom properties. goes in shared or portable class library. :
using xamarin.forms; namespace app1 { public class customsearchbar : searchbar { // utilize bindable properties maintain xaml binding compatibility public static readonly bindableproperty bartintproperty = bindableproperty.create<customsearchbar, color?>(p => p.bartint, null); public color? bartint { { homecoming (color?)getvalue(bartintproperty); } set { setvalue(bartintproperty, value); } } public static readonly bindableproperty searchstyleproperty = bindableproperty.create<customsearchbar, string>(p => p.searchstyle, "default"); public string searchstyle { { homecoming (string)getvalue(searchstyleproperty); } set { setvalue(searchstyleproperty, value); } } public static readonly bindableproperty barstyleproperty = bindableproperty.create<customsearchbar, string>(p => p.barstyle, "default"); public string barstyle { { homecoming (string)getvalue(barstyleproperty); } set { setvalue(barstyleproperty, value); } } } }
the sec class custom renderer itself, has access native uisearchbutton control. class goes in ios project:
using system; using system.drawing; using app1; using app1.ios; using monotouch.uikit; using xamarin.forms; using xamarin.forms.platform.ios; [assembly: exportrendererattribute(typeof(customsearchbar), typeof(customsearchbarrenderer))] namespace app1.ios { public class customsearchbarrenderer : searchbarrenderer { // there might improve place this, don't know public override void draw(rectanglef rect) { var csb = (customsearchbar) element; if (csb.bartint != null) control.bartintcolor = csb.bartint.getvalueordefault().touicolor(); control.barstyle = (uibarstyle)enum.parse(typeof(uibarstyle), csb.barstyle); control.searchbarstyle = (uisearchbarstyle)enum.parse(typeof(uisearchbarstyle), csb.barstyle); base.draw(rect); } } }
the code little rough, idea.
a couple of additional notes:
none of works without exportrendererattribute, don't leave off. i had create bartint nullable (because null default , color.default results in black). i override draw() though i'm not technically drawing because don't know else set code. command , element properties not available in constructor. if find improve solution, i'll seek update example. i utilize strings enum properties, improved in future. ios xamarin xamarin.forms
Comments
Post a Comment