如何在iOS中设置Xamarin.Forms SearchBar的样式?

Mic*_*ook 2 ios xamarin xamarin.forms

我正在尝试设置Xamarin.Forms SearchBar的样式,我可以看到有一个BackgroundColor属性,但无论我设置什么,该属性在iOS中被忽略.

甚至可以在iOS中定制Xamarin.Forms SearchBar(以及如何)?

小智 7

不幸的是,我无法得到公认的工作答案.对于iOS,以下代码对我有用.请注意,OnElementChanged而不是Draw似乎是在自定义渲染器中放置此类东西的首选位置.

using MonoTouch.UIKit;

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly:ExportRenderer( typeof(MyNamespace.MySearchBar), typeof(MyNamespace.iOS.MySearchBarRenderer_iOS))]


namespace MyNamespace.iOS
{
    public class MySearchBarRenderer_iOS : SearchBarRenderer
    {
        protected override void OnElementChanged( ElementChangedEventArgs<SearchBar> args )
        {
            base.OnElementChanged( args );

            UISearchBar bar = (UISearchBar)this.Control;

            bar.AutocapitalizationType = UITextAutocapitalizationType.None;
            bar.AutocorrectionType = UITextAutocorrectionType.No;
            bar.BarStyle = UIBarStyle.Default;
            bar.BarTintColor = UIColor.Green;
            bar.KeyboardType = UIKeyboardType.ASCIICapable;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)