使用Xamarin Effect删除Xamarin.iOS中TextEntry上的圆角

问题描述 投票:-1回答:2

使用带有TextEntry的Xamarin.Forms应用程序。它在iOS上呈现如下:

enter image description here

我正试图去掉圆角。因此我在iOS项目中添加了以下效果:

[assembly: ResolutionGroupName("Effects")]
[assembly: ExportEffect(typeof(EntryWithClearButtonEffect), "EntryWithClearButtonEffect")]
namespace C4S.MobileApp.iOS.Effects
{
    public class EntryWithClearButtonEffect : PlatformEffect
    {

        protected override void OnAttached()
        {
            ConfigureControl();
        }

        protected override void OnDetached()
        {
        }

        private void ConfigureControl()
        {
           var uiTextField = ((UITextField)Control);

           //Add iOS specific "clear button" to TextEntry
           uiTextField.ClearButtonMode = UITextFieldViewMode.WhileEditing;

           //Excpect to remove rounded corners
           uiTextField.Layer.CornerRadius = 0;
        }
    }
}

并在共享项目中使用它:

<Entry x:Name="SearchEntry" VerticalOptions="End" Placeholder="Suchen..." ReturnType="Done" IsTextPredictionEnabled="False"
               Focused="VisualElement_OnFocused"  Completed="Entry_OnCompleted" TextChanged="Entry_OnCompleted">
      <Entry.Effects>
             <customControls:EntryWithClearButton />
      </Entry.Effects>
</Entry>

不幸的是圆角仍然存在。还尝试将以下代码添加到ConfigureControl():

        uiTextField.ClipsToBounds = true;
        uiTextField.Layer.MasksToBounds = true;

也没影响。将UITextField.BorderStyle设置为None将删除整个边框。那不是我想要的。

编辑:

这就是TextEntry与Lucas Zhang所假设的CustomRenderer一样的样子 - MSFT:

enter image description here

存在rectengular形状的边界,但不幸的是圆角也是如此。顺便说一句,我测试了CustomRenderer和我上面的Effect。没有不同。我认为使用Effect是更好的选择(参见Why Use an Effect over a Custom Renderer? )。

ios xamarin.forms xamarin.ios uitextfield uikit
2个回答
0
投票

它将删除圆角

请参阅链接https://docs.microsoft.com/en-us/dotnet/api/uikit.uitextborderstyle?view=xamarin-ios-sdk-12

 uiTextField. UITextBorderStyle = UITextBorderStyle.None


0
投票

你可以使用CustomRenderer

在iOS项目中

using Foundation;
using UIKit;

using App7;
using App7.iOS;

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

[assembly: ExportRenderer(typeof(Entry), typeof(MyLabelRenderer))]
namespace App7.iOS
{
    public class MyLabelRenderer:EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if(Control!=null)
            {
                Control.Layer.MasksToBounds = true;
                Control.Layer.CornerRadius = 0;
                Control.Layer.BorderColor = UIColor.Black.CGColor;
                Control.Layer.BorderWidth = 1;
            }

        }

    }
}
<Entry x:Name="SearchEntry" VerticalOptions="End" Placeholder="Suchen..." ReturnType="Done" IsTextPredictionEnabled="False"
               Focused="VisualElement_OnFocused"  Completed="Entry_OnCompleted" TextChanged="Entry_OnCompleted">

</Entry>
© www.soinside.com 2019 - 2024. All rights reserved.