我正在尝试设置 Xamarin.Forms SearchBar 的样式,我可以看到有一个 BackgroundColor 属性,但是无论我设置什么,该属性在 iOS 中都会被忽略。
是否可以在 iOS 中自定义 Xamarin.Forms SearchBar(以及如何自定义)?
最佳答案
不幸的是,我无法获得公认的工作答案。尽管适用于 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;
}
}
}
关于ios - 如何在 iOS 中设置 Xamarin.Forms SearchBar 的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24390869/