iPhone上有类似于安卓Toast的功能吗?

13

当我编写 Android 应用时,我喜欢 Toast 功能。在使用 MonoTouch (C# .NET) 进行 iPhone 开发的过程中,是否有一种类似的“设置并忘记”的弹出消息方式呢?

13个回答

0

我对处理显示旋转的提示类进行了一点修改。

        public void Show ()
    {
        UIButton v = UIButton.FromType (UIButtonType.Custom);
        view = v;


        UIFont font = UIFont.SystemFontOfSize (16);
        SizeF textSize = view.StringSize (text, font, new SizeF (280, 60));

        UILabel label = new UILabel (new RectangleF (0, 0, textSize.Width + 5, textSize.Height + 5));
        label.BackgroundColor = UIColor.Clear;
        label.TextColor = UIColor.White;
        label.Font = font;
        label.Text = text;
        label.Lines = 0;
        label.ShadowColor = UIColor.DarkGray;
        label.ShadowOffset = new SizeF (1, 1);


        v.Frame = new RectangleF (0, 0, textSize.Width + 10, textSize.Height + 10);
        label.Center = new PointF (v.Frame.Size.Width / 2, v.Frame.Height / 2);
        v.AddSubview (label);

        v.BackgroundColor = UIColor.FromRGBA (0, 0, 0, 0.7f);
        v.Layer.CornerRadius = 5;

        UIWindow window = UIApplication.SharedApplication.Windows[0];

        PointF point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height / 2);

        if (theSettings.Gravity == ToastGravity.Top)
        {
            point = new PointF (window.Frame.Size.Width / 2, 45);
        }
        else if (theSettings.Gravity == ToastGravity.Bottom)
        {
            point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height - 45);
        }
        else if (theSettings.Gravity == ToastGravity.Center)
        {
            point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height / 2);
        }
        else
        {
            point = theSettings.Position;
        }

        point = new PointF (point.X + offsetLeft, point.Y + offsetTop);
        v.Center = point;
        //handle screen rotation
        float orientation=0;

        switch(UIApplication.SharedApplication.StatusBarOrientation)
        {
        case UIInterfaceOrientation.LandscapeLeft:
            orientation=-90;
            break;
        case UIInterfaceOrientation.LandscapeRight:
            orientation=90;
            break;
        case UIInterfaceOrientation.PortraitUpsideDown:
            orientation=180;
            break;
        }
        v.Transform=CGAffineTransform.MakeRotation ((float)(orientation / 180f * Math.Pi));
        window.AddSubview (v);
        v.AllTouchEvents += delegate { HideToast (); };

        NSTimer.CreateScheduledTimer (theSettings.DurationSeconds, HideToast);

    }

0
由于这个问题是针对Xamarin平台的,所以这里有几行可行的代码。_toast是一个私有类成员,ShowAlertInToast是一个公共成员。
public void ShowAlertInToast (string text)
{
                    _toast = new UIAlertView ("", text, (IUIAlertViewDelegate)null, null, null);
                    _toast.Show ();
        
                    //1.5 seconds
                    NSTimer.CreateScheduledTimer (new TimeSpan(0,0,0,0,1500), (obj) => { HideAlertInToast (); });
        
    }
        
     private void HideAlertInToast()
     {
                    if (_toast != null) {
                        _toast.DismissWithClickedButtonIndex (0, true);
                    }
     }

-1
我在Github上创建了一个新的仓库,其中包含一个用于iOS toast-style警报的类。我不喜欢code.google.com上的那个,它不能正确旋转并且不够美观。

https://github.com/esilverberg/ios-toast

享受吧。


让它依赖于庞大的Three20库会使其过于臃肿,我个人认为。 - Roger
我已经成功使用它一年多了,但当然每个项目都应该包含你内心的想法。 - esilver

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接