Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Toast in iPhone?

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)?

like image 779
Ian Vink Avatar asked Nov 19 '10 00:11

Ian Vink


People also ask

Is Toast available on iOS?

A toast message in iOS is a small, short-lived popup that provides a small bite of information to the users. In this iOS Swift tutorial, we will learn how to implement an iOS toast message in your iPhone app, but we will also show how to add animation to that.

What is similar to toast in iOS?

Toast in iOS In iOS, however, there's no such equivalent API. So one has to create one. Then we could call in the UIViewController, like below.

What are Android toasts?

A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Toasts automatically disappear after a timeout.

What is toast in iOS Swift?

Toast-Swift is a Swift extension that adds toast notifications to the UIView object class. It is intended to be simple, lightweight, and easy to use. Most toast notifications can be triggered with a single line of code. Toast-Swift is a native Swift port of Toast for iOS.


2 Answers

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 ();
        }

    }
}
like image 75
Ian Vink Avatar answered Oct 17 '22 07:10

Ian Vink


Check this out:

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

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

like image 36
clide313 Avatar answered Oct 17 '22 06:10

clide313