Gallery/Gallery.iOS/Renderers/AdaptedPageRenderer.cs
2021-08-11 14:09:03 +08:00

98 lines
3.2 KiB
C#

using Foundation;
using Gallery.iOS.Renderers;
using Gallery.Resources.UI;
using Gallery.Services;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(AdaptedPage), typeof(AdaptedPageRenderer))]
namespace Gallery.iOS.Renderers
{
public class AdaptedPageRenderer : PageRenderer
{
UIDeviceOrientation lastOrientation;
NSObject observer;
public override void ViewDidLoad()
{
base.ViewDidLoad();
if (Element is AdaptedPage page)
{
page.OnLoad();
}
}
protected override void Dispose(bool disposing)
{
if (Element is AdaptedPage page)
{
page.OnUnload();
}
base.Dispose(disposing);
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
var element = Element;
if (element != null)
{
var style = Environment.ConvertStyle(Screen.GetStatusBarStyle(element));
Environment.SetStatusBarStyle(style);
}
observer = UIDevice.Notifications.ObserveOrientationDidChange(OnOrientationChanged);
OnOrientationChanged(null, null);
}
public override void ViewWillDisappear(bool animated)
{
if (observer != null)
{
observer.Dispose();
observer = null;
}
base.ViewWillDisappear(animated);
}
private void OnOrientationChanged(object sender, NSNotificationEventArgs e)
{
var current = UIDevice.CurrentDevice.Orientation;
if (current == UIDeviceOrientation.FaceUp || current == UIDeviceOrientation.FaceDown)
{
return;
}
if (lastOrientation != current)
{
lastOrientation = current;
if (current == UIDeviceOrientation.Portrait && UIApplication.SharedApplication.StatusBarHidden)
{
var style = Environment.ConvertStyle(Screen.GetStatusBarStyle(Element));
if (style != UIStatusBarStyle.BlackOpaque)
{
UIApplication.SharedApplication.SetStatusBarHidden(false, true);
}
}
if (Element is AdaptedPage page)
{
AppShell.Current?.SetStatusBarHeight(
NavigationController?.NavigationBar.Frame.Height ?? 0,
UIApplication.SharedApplication.StatusBarFrame.Height);
var landscape =
current == UIDeviceOrientation.LandscapeLeft ||
current == UIDeviceOrientation.LandscapeRight;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
{
landscape |= current == UIDeviceOrientation.PortraitUpsideDown;
}
page.OnOrientationChanged(landscape);
}
}
}
}
}