Skip to content

Instantly share code, notes, and snippets.

@pokk
Created October 28, 2016 15:03
Show Gist options
  • Save pokk/43c8c4bd1d1a1a132763c34b310f58a0 to your computer and use it in GitHub Desktop.
Save pokk/43c8c4bd1d1a1a132763c34b310f58a0 to your computer and use it in GitHub Desktop.
create a dialog fragment in iOS Xamarin

Introduction

Create a simple dialog toast in iOS by Xamarin.

/// <summary>
/// Creates the checked toast.
/// </summary>
/// <param name="sec">Sec for dialog appearing time.</param>
/// <param name="action">After disappear, the action what you want to do.</param>
public static void CreateCheckedToast(double sec, Action action = null)
{
ToastViewController view = new ToastViewController();
view.View.Frame = new CGRect(new CGPoint(0, 0), new CGSize(view.View.Bounds.Size.Width, view.View.Bounds.Size.Height));
showInTheCenter(view);
// Assign the action after the toast is closed.
Action<NSTimer> handler = (o) => view.eventCloseDialog(action);
NSTimer.CreateScheduledTimer(sec, handler);
handler = null;
}
/// <summary>
/// Creates the loading toast.
/// </summary>
public static void CreateLoadingToast()
{
// Background color
loadBackground = new UIView();
loadBackground.Frame = new CGRect(new CGPoint(0, 0), new CGSize(topViewController().View.Frame.Width, topViewController().View.Frame.Height));
loadBackground.BackgroundColor = UIColor.White;
loadBackground.Alpha = 0.3f;
topViewController().View.AddSubview(loadBackground);
ToastViewController view = new ToastViewController(toastType.LoadIcon);
view.View.Frame = new CGRect(new CGPoint(0, 0), new CGSize(view.View.Bounds.Size.Width, view.View.Bounds.Size.Height));
showInTheCenter(view);
// Assign the action.
loadCompleteHandler = (o) =>
{
view.ivLoadCompleted.Hidden = false;
view.wvLoading.Hidden = true;
};
loadCloseHandler = (o) =>
{
view.eventCloseDialog(null);
// Close the dialog with background.
UIView.Animate(0.4f, () =>
{
loadBackground.Alpha = 0;
}, () =>
{
loadBackground.RemoveFromSuperview();
loadBackground.Dispose();
loadBackground = null;
});
};
}
/// <summary>
/// Closes the loading toast.
/// </summary>
public static void CloseLoadingToast()
{
NSTimer.CreateScheduledTimer(0, loadCompleteHandler);
NSTimer.CreateScheduledTimer(2, loadCloseHandler);
loadCompleteHandler = null;
loadCloseHandler = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment