Android Toast in iPhone?

2019-01-14 12:57发布

When I write Android apps, I love the Toast feature. Is there a way to get this kind of set and forget popup message in iPhone development using MonoTouch (C# .NET)?

12条回答
SAY GOODBYE
2楼-- · 2019-01-14 13:34

You can use this link for objective-c code for Toast

http://code.google.com/p/toast-notifications-ios/source/browse/trunk/

While this link for its usage

http://code.google.com/p/toast-notifications-ios/wiki/HowToUse

which could be like any one of the below samples

[[iToast makeText:NSLocalizedString(@"The activity has been successfully saved.", @"")] show];

[[[iToast makeText:NSLocalizedString(@"The activity has been successfully saved.", @"")] 
                          setGravity:iToastGravityBottom] show];

[[[[iToast makeText:NSLocalizedString(@"Something to display a very long time", @"")] 
                  etGravity:iToastGravityBottom] setDuration:iToastDurationLong] show];
查看更多
家丑人穷心不美
3楼-- · 2019-01-14 13:36

I created a new repo on github with a class to do iOS toast-style alerts. I didn't like the one on code.google.com, it didn't rotate properly and wasn't pretty.

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

Enjoy folks.

查看更多
趁早两清
4楼-- · 2019-01-14 13:37

Here's my version: http://github.com/scalessec/toast

I think it's simpler to use because it's implemented as a obj-c category, thereby adding the makeToast methods to any instance of UIView. eg:

[self.view makeToast:@"This is some message as toast."
            duration:3.0
            position:@"bottom"];
查看更多
再贱就再见
5楼-- · 2019-01-14 13:39

Check this out:

https://github.com/ecstasy2/toast-notifications-ios

Edit: The project has moved to github so i update the link.

查看更多
在下西门庆
6楼-- · 2019-01-14 13:42

I really like MonoTouch solution proposed by Bahai.

The following is not a substitution. Is just a ready-to-go one method the worked for me.

    private async Task ShowToast(string message, UIAlertView toast = null)
    {
        if (null == toast)
        {
            toast = new UIAlertView(null, message, null, null, null);
            toast.Show();
            await Task.Delay(2000);
            await ShowToast(message, toast);
            return;
        }

        UIView.BeginAnimations("");
        toast.Alpha = 0;
        UIView.CommitAnimations();
        toast.DismissWithClickedButtonIndex(0, true);
    }

If the method is called from a background thread (not the main UI thread) then BeginInvokeOnMainThread is required which means just call it like this.

BeginInvokeOnMainThread(() =>
{
 ShowToast(message);
});
查看更多
Anthone
7楼-- · 2019-01-14 13:46

MonoTouch Toast Version here. Inspired by Android.

To call it,

        ToastView t = new ToastView ("Email Sent", 1000);
        t.Show ();

Enum File:

public enum ToastGravity
{
    Top = 0,
    Bottom = 1,
    Center = 2
}

ToastSettings File:

using System;
using System.Drawing;
using MonoTouch.UIKit;
namespace General
{

    public class ToastSettings
    {
        public ToastSettings ()
        {
            this.Duration = 500;
            this.Gravity = ToastGravity.Center;
        }

        public int Duration
        {
            get;
            set;
        }

        public double DurationSeconds
        {
            get { return (double) Duration/1000 ;}

        }

        public ToastGravity Gravity
        {
            get;
            set;
        }

        public PointF Position
        {
            get;
            set;
        }


    }
}

Main Toast Class:

using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.ObjCRuntime;

namespace General
{
    public class ToastView : NSObject
    {

        ToastSettings theSettings = new ToastSettings ();

        private string text = null;
        UIView view;
        public ToastView (string Text, int durationMilliseonds)
        {
            text = Text;
            theSettings.Duration = durationMilliseonds;
        }

        int offsetLeft = 0;
        int offsetTop = 0;
        public ToastView SetGravity (ToastGravity gravity, int OffSetLeft, int OffSetTop)
        {
            theSettings.Gravity = gravity;
            offsetLeft = OffSetLeft;
            offsetTop = OffSetTop;
            return this;
        }

        public ToastView SetPosition (PointF Position)
        {
            theSettings.Position = Position;
            return this;
        }

        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;
            window.AddSubview (v);
            v.AllTouchEvents += delegate { HideToast (null); };

            NSTimer.CreateScheduledTimer (theSettings.DurationSeconds, HideToast);

        }


        void HideToast ()
        {
            UIView.BeginAnimations ("");
            view.Alpha = 0;
            UIView.CommitAnimations ();
        }

        void RemoveToast ()
        {
            view.RemoveFromSuperview ();
        }

    }
}
查看更多
登录 后发表回答