4
votes

I'm writing an application for iOS in Xamarin (C#).

What is the equivalent of Android's 'toast notifications' in iOS?

From documentation:

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. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.

For example in Android (C#) I can show them like this:

Toast.MakeText(this, "Added " + counttext.Text +" pcs to cart" , ToastLength.Long).Show();

How do I write the same for iOS?

Thank's for answer.

4

4 Answers

9
votes

In Xamarin iOS you have to use custom designed UIView with animation to achieve the same effect

public void ShowToast(String message, UIView view)
    {
        UIView residualView = view.ViewWithTag(1989);
        if (residualView != null)
            residualView.RemoveFromSuperview();

        var viewBack = new UIView(new CoreGraphics.CGRect(83, 0, 300, 100));
        viewBack.BackgroundColor = UIColor.Black;
        viewBack.Tag = 1989;
        UILabel lblMsg = new UILabel(new CoreGraphics.CGRect(0, 20, 300, 60));
        lblMsg.Lines = 2;
        lblMsg.Text = message;
        lblMsg.TextColor = UIColor.White;
        lblMsg.TextAlignment = UITextAlignment.Center;
        viewBack.Center = view.Center;
        viewBack.AddSubview(lblMsg);
        view.AddSubview(viewBack);
        roundtheCorner(viewBack);
        UIView.BeginAnimations("Toast");
        UIView.SetAnimationDuration(3.0f);
        viewBack.Alpha = 0.0f;
        UIView.CommitAnimations();
    }
3
votes

Use the Xamarin built-in component BigTed

https://components.xamarin.com/view/btprogresshud

BTProgressHUD.ShowToast("Hello from Toast");

2
votes

You can try https://github.com/andrius-k/Toast

It is available as nuget package Toast.ios
usage:

// import
using GlobalToast;
Toast.MakeToast("message").SetDuration(0.5).Show();
0
votes

As of iOS 9, the UIAlertController class is recommended for displaying feedback to the user.

Example usage:

var alertController = UIAlertController.Create("Your Count", "Added 1 PC", UIAlertControllerStyle.Alert);

alertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, (action) => Console.WriteLine("OK Clicked.")));

PresentViewController(alertController, true, null);

One thing to bare in mind is that you must be in a UIViewController in order to present the alert. This is due to the fact that a UIAlertController is a subclass of a UIViewController.