From 54e1e4a56c1e77b395f9f3830ccb2b81b581cdc0 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Fri, 18 Nov 2016 12:47:52 -0700 Subject: Have PresentAlert hide new window when alert is dismissed (#543) --- Xamarin.Forms.Platform.iOS/Platform.cs | 130 +++++++++++++++++++++------------ 1 file changed, 82 insertions(+), 48 deletions(-) (limited to 'Xamarin.Forms.Platform.iOS') diff --git a/Xamarin.Forms.Platform.iOS/Platform.cs b/Xamarin.Forms.Platform.iOS/Platform.cs index 762ccf00..f39856b8 100644 --- a/Xamarin.Forms.Platform.iOS/Platform.cs +++ b/Xamarin.Forms.Platform.iOS/Platform.cs @@ -48,14 +48,7 @@ namespace Xamarin.Forms.Platform.iOS if (Forms.IsiOS8OrNewer) { - var alert = UIAlertController.Create(arguments.Title, arguments.Message, UIAlertControllerStyle.Alert); - var oldFrame = alert.View.Frame; - alert.View.Frame = new RectangleF(oldFrame.X, oldFrame.Y, oldFrame.Width, oldFrame.Height - _alertPadding * 2); - alert.AddAction(UIAlertAction.Create(arguments.Cancel, UIAlertActionStyle.Cancel, a => arguments.SetResult(false))); - if (arguments.Accept != null) - alert.AddAction(UIAlertAction.Create(arguments.Accept, UIAlertActionStyle.Default, a => arguments.SetResult(true))); - - PresentAlert(alert); + PresentAlert(arguments); } else { @@ -82,45 +75,7 @@ namespace Xamarin.Forms.Platform.iOS if (Forms.IsiOS8OrNewer) { - var alert = UIAlertController.Create(arguments.Title, null, UIAlertControllerStyle.ActionSheet); - - if (arguments.Cancel != null) - { - alert.AddAction(UIAlertAction.Create(arguments.Cancel, UIAlertActionStyle.Cancel, a => arguments.SetResult(arguments.Cancel))); - } - - if (arguments.Destruction != null) - { - alert.AddAction(UIAlertAction.Create(arguments.Destruction, UIAlertActionStyle.Destructive, a => arguments.SetResult(arguments.Destruction))); - } - - foreach (var label in arguments.Buttons) - { - if (label == null) - continue; - - var blabel = label; - alert.AddAction(UIAlertAction.Create(blabel, UIAlertActionStyle.Default, a => arguments.SetResult(blabel))); - } - - if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) - { - UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications(); - var observer = NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification, - n => { alert.PopoverPresentationController.SourceRect = pageRenderer.ViewController.View.Bounds; }); - - arguments.Result.Task.ContinueWith(t => - { - NSNotificationCenter.DefaultCenter.RemoveObserver(observer); - UIDevice.CurrentDevice.EndGeneratingDeviceOrientationNotifications(); - }, TaskScheduler.FromCurrentSynchronizationContext()); - - alert.PopoverPresentationController.SourceView = pageRenderer.ViewController.View; - alert.PopoverPresentationController.SourceRect = pageRenderer.ViewController.View.Bounds; - alert.PopoverPresentationController.PermittedArrowDirections = 0; // No arrow - } - - PresentAlert(alert); + PresentAlert(arguments, pageRenderer); } else { @@ -430,11 +385,90 @@ namespace Xamarin.Forms.Platform.iOS return Page == page || _modals.Contains(page); } - void PresentAlert(UIAlertController alert) + // Creates a UIAlertAction which includes a call to hide the presenting UIWindow at the end + UIAlertAction CreateActionWithWindowHide(string text, UIAlertActionStyle style, Action setResult, UIWindow window) + { + return UIAlertAction.Create(text, style, + a => + { + setResult(); + window.Hidden = true; + }); + } + + void PresentAlert(AlertArguments arguments) + { + var window = new UIWindow { BackgroundColor = Color.Transparent.ToUIColor() }; + + var alert = UIAlertController.Create(arguments.Title, arguments.Message, UIAlertControllerStyle.Alert); + var oldFrame = alert.View.Frame; + alert.View.Frame = new RectangleF(oldFrame.X, oldFrame.Y, oldFrame.Width, oldFrame.Height - _alertPadding * 2); + + if (arguments.Cancel != null) + { + alert.AddAction(CreateActionWithWindowHide(arguments.Cancel, UIAlertActionStyle.Cancel, + () => arguments.SetResult(false), window)); + } + + if (arguments.Accept != null) + { + alert.AddAction(CreateActionWithWindowHide(arguments.Accept, UIAlertActionStyle.Default, + () => arguments.SetResult(true), window)); + } + + PresentAlert(window, alert); + } + + void PresentAlert(ActionSheetArguments arguments, IVisualElementRenderer pageRenderer) { + var alert = UIAlertController.Create(arguments.Title, null, UIAlertControllerStyle.ActionSheet); var window = new UIWindow { BackgroundColor = Color.Transparent.ToUIColor() }; + + if (arguments.Cancel != null) + { + alert.AddAction(CreateActionWithWindowHide(arguments.Cancel, UIAlertActionStyle.Cancel, () => arguments.SetResult(arguments.Cancel), window)); + } + + if (arguments.Destruction != null) + { + alert.AddAction(CreateActionWithWindowHide(arguments.Destruction, UIAlertActionStyle.Destructive, () => arguments.SetResult(arguments.Destruction), window)); + } + + foreach (var label in arguments.Buttons) + { + if (label == null) + continue; + + var blabel = label; + + alert.AddAction(CreateActionWithWindowHide(blabel, UIAlertActionStyle.Default, () => arguments.SetResult(blabel), window)); + } + + if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) + { + UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications(); + var observer = NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification, + n => { alert.PopoverPresentationController.SourceRect = pageRenderer.ViewController.View.Bounds; }); + + arguments.Result.Task.ContinueWith(t => + { + NSNotificationCenter.DefaultCenter.RemoveObserver(observer); + UIDevice.CurrentDevice.EndGeneratingDeviceOrientationNotifications(); + }, TaskScheduler.FromCurrentSynchronizationContext()); + + alert.PopoverPresentationController.SourceView = pageRenderer.ViewController.View; + alert.PopoverPresentationController.SourceRect = pageRenderer.ViewController.View.Bounds; + alert.PopoverPresentationController.PermittedArrowDirections = 0; // No arrow + } + + PresentAlert(window, alert); + } + + static void PresentAlert(UIWindow window, UIAlertController alert) + { window.RootViewController = new UIViewController(); window.RootViewController.View.BackgroundColor = Color.Transparent.ToUIColor(); + window.WindowLevel = UIWindowLevel.Alert + 1; window.MakeKeyAndVisible(); window.RootViewController.PresentViewController(alert, true, null); } -- cgit v1.2.3