【问题标题】:Shadow in UIView cannot overlay Entry or Editor or Picker in Xamarin FormsUIView 中的阴影无法覆盖 Xamarin 表单中的条目或编辑器或选取器
【发布时间】:2019-04-24 20:07:01
【问题描述】:

我在 Xamarin Form 项目中添加了 RoutingEffect,在我的 Xamarin.iOS 项目中添加了 PlatformEffect。它将为 Stacklayout 添加效果。本演示中的 Stacklayout 是一个自定义导航栏。导航栏下方是一个滚动视图,有许多单元格(标签、条目、选择器)。

我在Android中实现是OK的。 但是在iOS中存在问题:阴影效果不能覆盖一些控件,例如:Entry、Editor、Picker。你能分享我如何解决它吗? 这是 Xamarin.iOS 项目中的代码。

public class DropShadowEffect : PlatformEffect
{
    protected override void OnAttached()
    {
        try
        {
            var effect = (myDemo.UIControls.DropShadowEffect)Element.Effects.FirstOrDefault(e => e is myDemo.UIControls.DropShadowEffect);

            if (effect != null)
            {
                Container.Layer.CornerRadius = effect.Radius;
                Container.Layer.ShadowColor = UIColor.Red.CGColor;// effect.Color.ToCGColor();
                Container.Layer.ShadowOffset = new CGSize(effect.DistanceX, effect.DistanceY);
                Container.Layer.ShadowOpacity = 0.8f;
                Container.Layer.ShadowRadius = 2f;
                Container.Layer.ShouldRasterize = true;
                Container.Layer.MasksToBounds = false;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Cannot set property on attached control. Error: {0}", ex.Message);
        }
    }

*阴影效果过度Label是可以的

*阴影效果不能覆盖 Picker 或 Entry

【问题讨论】:

    标签: ios xamarin.forms xamarin.ios effects dropshadow


    【解决方案1】:

    原因:

    其实像Label这样的阴影还是会叠加的,但是好像不是很明显。如果你设置了label的背景(比如red),就会看到叠加了。

    解决方案:

    可以在自定义渲染器中设置PickerEntryBackgroundColor,让alpha为0。

    例如EntryRenderer

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
    
            if (Control != null)
            {
                Control.BackgroundColor = new UIColor(1,1,1,0);//The last parameter sets the alpha of backgound as transparent
                Control.Layer.MasksToBounds = true;
                Control.Layer.CornerRadius = xxx;  //set the rounded corner
                Control.Layer.BorderColor = UIColor.xxx.CGColor;
                Control.Layer.BorderWidth = xxx;           
            }
    
        }
    

    【讨论】:

    • 谢谢!我尝试了这个解决方案,但我无法设置条目或选择器的背景颜色。它总是透明的。您还有其他解决方案吗?
    • 为什么不能设置?
    • 因为我将 alpha 0 设置为 BackgroundColor 是透明的。如果我不设置 alpha 0,则存在有关阴影的问题。私人无效 SetBackgroundColor() { Control.BackgroundColor = new UIColor( (nfloat)FormElement.BackgroundColor.R, (nfloat)FormElement.BackgroundColor.G, (nfloat)FormElement.BackgroundColor.B, 0); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2018-03-28
    • 1970-01-01
    相关资源
    最近更新 更多