Skip to content

Commit

Permalink
Fix controls on ios
Browse files Browse the repository at this point in the history
  • Loading branch information
martijn00 committed Jan 12, 2023
1 parent 3e0da7f commit 1c7141e
Showing 1 changed file with 83 additions and 14 deletions.
97 changes: 83 additions & 14 deletions MediaManager/Platforms/Ios/Video/VideoView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ public AVPlayerViewController PlayerViewController
if (_playerViewController != null)
{
_playerViewController.View.Frame = Bounds;

//IOS16 or higher

//var viewController = WindowStateManager.Default.GetCurrentUIViewController() ?? throw new InvalidOperationException("ViewController can't be null.");

//if (viewController.View is null)
//{
// throw new NullReferenceException($"{nameof(viewController.View)} cannot be null");
//}
#if IOS16_0_OR_GREATER

//// Zero out the safe area insets of the AVPlayerViewController
//UIEdgeInsets insets = viewController.View.SafeAreaInsets;
//_playerViewController.AdditionalSafeAreaInsets =
// new UIEdgeInsets(insets.Top * -1, insets.Left, insets.Bottom * -1, insets.Right);
var viewController = GetCurrentUIViewController() ?? throw new InvalidOperationException("ViewController can't be null.");

//// Add the View from the AVPlayerViewController to the parent ViewController
//viewController.View.AddSubview(_playerViewController.View);
if (viewController.View is null)
{
throw new NullReferenceException($"{nameof(viewController.View)} cannot be null");
}

// Zero out the safe area insets of the AVPlayerViewController
UIEdgeInsets insets = viewController.View.SafeAreaInsets;
_playerViewController.AdditionalSafeAreaInsets =
new UIEdgeInsets(insets.Top * -1, insets.Left, insets.Bottom * -1, insets.Right);

// Add the View from the AVPlayerViewController to the parent ViewController
viewController.View.AddSubview(_playerViewController.View);
#endif
AddSubview(_playerViewController.View);
(Superview?.NextResponder as UIViewController)?.AddChildViewController(_playerViewController);
}
Expand Down Expand Up @@ -82,5 +82,74 @@ public virtual void InitView()
if (MediaManager.MediaPlayer.AutoAttachVideoView)
MediaManager.MediaPlayer.VideoView = this;
}

public UIViewController? GetCurrentUIViewController()
{
UIViewController viewController = null;

var window = GetKeyWindow();

if (window != null && window.WindowLevel == UIWindowLevel.Normal)
viewController = window.RootViewController;

if (viewController == null)
{
window = GetWindows()?
.OrderByDescending(w => w.WindowLevel)
.FirstOrDefault(w => w.RootViewController != null && w.WindowLevel == UIWindowLevel.Normal);

viewController = window?.RootViewController;
}

while (viewController?.PresentedViewController != null)
viewController = viewController.PresentedViewController;

return viewController;
}

public UIWindow? GetCurrentUIWindow()
{
var window = GetKeyWindow();

if (window != null && window.WindowLevel == UIWindowLevel.Normal)
return window;

if (window == null)
{
window = GetWindows()?
.OrderByDescending(w => w.WindowLevel)
.FirstOrDefault(w => w.RootViewController != null && w.WindowLevel == UIWindowLevel.Normal);
}

return window;
}

static UIWindow? GetKeyWindow()
{
// if we have scene support, use that
if (OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
{
var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows.FirstOrDefault();
}

// use the windows property (up to 13.0)
return UIApplication.SharedApplication.KeyWindow;
}

static UIWindow[]? GetWindows()
{
// if we have scene support, use that
if (OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
{
var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows;
}

// use the windows property (up to 15.0)
return UIApplication.SharedApplication.Windows;
}
}
}

0 comments on commit 1c7141e

Please sign in to comment.