Toast notification Xamarin iOS

11,876

Solution 1

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

Solution 2

Use the Xamarin built-in component BigTed

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

BTProgressHUD.ShowToast("Hello from Toast");

Solution 3

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();
Share:
11,876
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

  • Sonali
    Sonali about 7 years
    How to pass UIView if interface is defined in Portable and its implementation is in ios project, and I am trying to show toast notification from portable?
  • Durai Amuthan.H
    Durai Amuthan.H about 7 years
    This one is not meant to be shown from Portable.You can show this from Xamarin.iOS side
  • Sonali
    Sonali about 7 years
    Suppose i have made interface in portable and its implementation in Ios project then how can I pass UIview to this?
  • Durai Amuthan.H
    Durai Amuthan.H about 7 years
    @Sonali - I never tried this before ... Portable is supposed to be platform independent so having iOS Specific code over there will contradict the very purpose.You can use this on xamarin.iOS side and I don't think that would be hard
  • Paolo Mastrangelo
    Paolo Mastrangelo over 6 years
    "What is the equivalent of Android's 'toast notifications' in iOS?" I'm sorry, but this is not the equivalent, for dismiss it you have to do an action.
  • Entretoize
    Entretoize over 5 years
    The simplest solution I found and for roundTheCorner use viewBack.Layer.CornerRadius=10; viewBack.Layer.MasksToBounds=true;
  • Swati
    Swati over 4 years
    tried with this but getting error to use Toast. Getting null reference