有什么方法可以使用材质视觉在 iOS 中自定义控件样式吗?

我正在尝试Entry为 iOS 平台自Visual=Material定义一个已启用的字段。


我尝试过CustomRenderer,但由于是 iOS 平台,我不知道如何实现,例如,修改材质底部边框颜色而不修改控件的整个文本颜色。


[assembly: ExportRenderer(typeof(Entry), typeof(CustomMaterialEntryRenderer), new[] { typeof(VisualMarker.MaterialVisual) })]


    public class CustomMaterialEntryRenderer : MaterialEntryRenderer

    {

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)

        {

            base.OnElementChanged(e);


            if (Control == null || e.NewElement == null) return;


            Layer.BorderColor = Color.FromHex("#cedee7").ToCGColor();

        }

    }

为了足够清楚,以防万一,我想要底线为红色,文本为黑色。

https://img3.mukewang.com/650ffebd00016b1106480146.jpg

梦里花落0921
浏览 69回答 1
1回答

扬帆大鱼

这似乎是一个永远不会被调用的现有问题CustomRenderer。我们将重点关注这个问题。解决方法1:如果你只是想设置 的下划线颜色Entry。无需设置Visual=Material,只需创建一个默认的Custom Renderer即可Entry。if (Control != null){  Control.BorderStyle = UITextBorderStyle.None;   UIView lineView = new UIView()  {     Frame = new CGRect(0, Element.HeightRequest - 1, Element.WidthRequest, 1),     BackgroundColor = UIColor.Red,  };   Control.AddSubview(lineView); }不要忘记在 xaml 中设置WidthRequestand HeightRequest。解决方法2幸运的是,nuget 有很多 Material Controls 插件。并且您可以直接下载并使用。例如MaterialFormControls从 Nuget Manager 下载软件包(确保选中包含预发布)并设置属性 AccentColor 来更改下划线颜色<local:MaterialEntry IsPassword="True" Placeholder="email" AccentColor="Red"/>
打开App,查看更多内容
随时随地看视频慕课网APP