简体   繁体   English

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

[英]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: 另外两点说明:

  • None of this works without the ExportRendererAttribute, so don't leave that off. 如果没有ExportRendererAttribute,这些都不起作用,所以不要将其关闭。
  • I had to make the BarTint nullable (because it is null be default and Color.Default results in black). 我必须使BarTint可以为空(因为它是null默认值,Color.Default结果为黑色)。
  • I override Draw() even though I'm not technically drawing anything because I don't know where else to put the code. 我重写Draw(),即使我在技术上没有绘制任何东西,因为我不知道在哪里放置代码。 The Control and Element properties are not available in the constructor. Control和Element属性在构造函数中不可用。 If I find a better solution, I'll try to update this example. 如果我找到更好的解决方案,我会尝试更新此示例。
  • I use strings for the Enum properties, but that could probably be improved in the future. 我使用字符串作为Enum属性,但将来可能会改进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IOS上的Xamarin.Forms:删除SearchBar和NavigationBar之间的空间 - Xamarin.Forms on IOS: Remove space between SearchBar and NavigationBar 为什么我无法归档 Xamarin.Forms iOS 应用程序? - Why can't I archive a Xamarin.Forms iOS application? 如何访问Xamarin.Forms中Resources(iOS)文件夹的文件路径? - How can I access the filepath to Resources (iOS) folder in Xamarin.Forms? Xamarin.Forms iOS 消息在打开的弹出窗口后面呈现。 如何设置视图的 z 轴? - Xamarin.Forms iOS message is rendered BEHIND open popups. How can I set the z axis of a view? 如何在 Xamarin.Forms 中获取 iOS 和 Android 的辅助功能字体大小,以便我可以在 HTML WebView 中更改字体大小? - How to get the accessibility font size for iOS and Android in Xamarin.Forms so I can change font-size in a HTML WebView? Xamarin.Forms iOS 中的 DateTimePicker - DateTimePicker in Xamarin.Forms iOS 如何为Xamarin.Forms中的特定控件定义样式资源? - How to define style resources for specific control in Xamarin.Forms? Xamarin.Forms iOS无法自定义通知 - Xamarin.Forms iOS Can't Customize Notifications 如何在Xamarin.Forms中为IOS平台创建更改UITableViewCellSelectionStyle - How do I create change the UITableViewCellSelectionStyle for IOS platform in Xamarin.Forms 如何使用 Xamarin.Z6450242331912982ACA6663 下载公开可用的 Android X 和 iOS 上的文件? - How do I download a file on both Android X and iOS that is publicly available, using Xamarin.Forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM