Xamarin.Forms iOS 消息呈现在打开的弹出窗口之后如何设置视图的 z 轴?

问题描述

我正在使用这个自定义组件在 iOS 上显示消息(有点像 android 中的祝酒词)。

在 android 中,toast 总是呈现在顶部,但在 iOS 中,如果打开了一个 popUP,则消息呈现在布局后面:

    void ShowAlert(string message,double seconds)
    {
        Device.BeginInvokeOnMainThread(() => {
            alertDelay = NSTimer.CreateScheduledTimer(seconds,(obj) =>
            {
                dismissMessage();
            });
            alert = UIAlertController.Create("",message,UIAlertControllerStyle.Alert);
            var window = UIApplication.SharedApplication.Delegate.Getwindow();
            window.RootViewController.PresentViewController(alert,true,null);
        });
    }

如何设置视图的 z 轴,使其为页面 -> 弹出窗口 -> 消息。

谢谢

解决方法

尝试通过创建自定义“弹出”UIView 来实现它。

只需定义一个自定义 UIView 来显示“警报”,然后 toast 将显示在最顶部。

MyPopupView 类:

class MyPopupView : UIView
{
    public MyPopupView(string message,string buttonText)
    {
        this.Frame = new CGRect(50,UIScreen.MainScreen.Bounds.Height / 2 - 30,UIScreen.MainScreen.Bounds.Width - 100,80);
        this.Layer.CornerRadius = 8;
        this.BackgroundColor = UIColor.FromRGB(230,230,230);

        UILabel label_Message = new UILabel();
        label_Message.Text = message;
        label_Message.Frame = new CGRect(20,10,200,20);
        UIButton button_OK = new UIButton();
        button_OK.SetTitle(buttonText,UIControlState.Normal);
        button_OK.SetTitleColor(UIColor.Red,UIControlState.Normal);
        button_OK.Frame = new CGRect(this.Bounds.Width / 2 - 30,this.Bounds.Height / 2 + 10,60,20);
        button_OK.TouchUpInside += Button_OK_TouchUpInside;

        this.AddSubview(label_Message);
        this.AddSubview(button_OK);
    }

    private void Button_OK_TouchUpInside(object sender,EventArgs e)
    {
        this.RemoveFromSuperview();
    }
}

添加 MyPopupView:

MyPopupView myPopupView = new MyPopupView("A message here!","I know");
this.View.AddSubview(myPopupView);

FWIW,在我的测试中,我使用了 Toast.iOS