[英]How can I style the Xamarin.Forms SearchBar in iOS?
I'm trying to style the Xamarin.Forms SearchBar and I can see that there is a BackgroundColor property, however no matter what I set, the property is ignored in iOS. 我正在尝试设置Xamarin.Forms SearchBar的样式,我可以看到有一个BackgroundColor属性,但无论我设置什么,该属性在iOS中被忽略。
Is it even possible to customize the Xamarin.Forms SearchBar in iOS (and how)? 甚至可以在iOS中定制Xamarin.Forms SearchBar(以及如何)?
I couldn't get the accepted answer to work unfortunately. 不幸的是,我无法得到公认的工作答案。 The below code does work for me though for iOS. 对于iOS,以下代码对我有用。 Note that OnElementChanged rather than Draw seems to be the preferred place to put this sort of thing in custom renderers. 请注意,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;
}
}
}
As near as I can tell, Xamarin.Forms does not property implement the BackgroundColor property, or it is broken. 就像我所知,Xamarin.Forms没有属性实现BackgroundColor属性,或者它被破坏了。 The UISearchBar property closest to a true background color is BarTint, which is not set by XForms. 最接近真实背景颜色的UISearchBar属性是BarTint,它不是由XForms设置的。
To solve this, I took the comments to heart and created my own custom SearchBar with a custom renderer in order to extend the BarTint property as well as a few other things I wanted. 为了解决这个问题,我把评论放在心上并用自定义渲染器创建了我自己的自定义SearchBar,以便扩展BarTint属性以及我想要的其他一些东西。
Note: In order to use custom renderers make sure you are updated to Xamarin.Forms 1.1.1.6206 or greater. 注意:要使用自定义渲染器,请确保更新为Xamarin.Forms 1.1.1.6206或更高版本。 To update your platform version use NuGet in Visual Studio, or the built in package manager in XamarinStudio. 要更新平台版本,请使用Visual Studio中的NuGet或XamarinStudio中的内置包管理器。
You will need two classes, the first one is CustomSearchBar, which is what the UI will use to hold the custom properties. 您将需要两个类,第一个是CustomSearchBar,这是UI将用于保存自定义属性的类。 It goes in the shared or portable class library. 它位于共享或可移植类库中。 : :
using Xamarin.Forms;
namespace App1
{
public class CustomSearchBar : SearchBar
{
// Use Bindable properties to maintain XAML binding compatibility
public static readonly BindableProperty BarTintProperty = BindableProperty.Create<CustomSearchBar, Color?>(p => p.BarTint, null);
public Color? BarTint
{
get { return (Color?)GetValue(BarTintProperty); }
set { SetValue(BarTintProperty, value); }
}
public static readonly BindableProperty SearchStyleProperty = BindableProperty.Create<CustomSearchBar, string>(p => p.SearchStyle, "Default");
public string SearchStyle
{
get { return (string)GetValue(SearchStyleProperty); }
set { SetValue(SearchStyleProperty, value); }
}
public static readonly BindableProperty BarStyleProperty = BindableProperty.Create<CustomSearchBar, string>(p => p.BarStyle, "Default");
public string BarStyle
{
get { return (string)GetValue(BarStyleProperty); }
set { SetValue(BarStyleProperty, value); }
}
}
}
The second class is the custom renderer itself, which has access to the native UISearchButton control. 第二个类是自定义渲染器本身,它可以访问本机UISearchButton控件。 This class goes in the iOS project: 这个课程在iOS项目中进行:
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 be a better place for this, but I don't know where it is
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 is a little rough, but hopefully you get the idea. 代码有点粗糙,但希望你能得到这个想法。
A couple of additional notes: 另外两点说明:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.