diff --git a/src/Core/src/Handlers/Page/PageHandler.iOS.cs b/src/Core/src/Handlers/Page/PageHandler.iOS.cs index 31c1aceddadb..f7cd704ff39b 100644 --- a/src/Core/src/Handlers/Page/PageHandler.iOS.cs +++ b/src/Core/src/Handlers/Page/PageHandler.iOS.cs @@ -22,9 +22,6 @@ protected override PageView CreateNativeView() _pageViewController = new PageViewController(VirtualView, this.MauiContext); - if (_pageViewController.CurrentNativeView == null) - _pageViewController.LoadNativeView(); - if (_pageViewController.CurrentNativeView is PageView pv) return pv; diff --git a/src/Core/src/Platform/iOS/ContainerViewController.cs b/src/Core/src/Platform/iOS/ContainerViewController.cs index 275942ce8849..eb08014671ff 100644 --- a/src/Core/src/Platform/iOS/ContainerViewController.cs +++ b/src/Core/src/Platform/iOS/ContainerViewController.cs @@ -11,10 +11,19 @@ public IView? CurrentView get => _view; set => SetView(value); } + + public UIView? CurrentNativeView + => _pendingLoadedView ?? currentNativeView; + public IMauiContext? Context { get; set; } - internal UIView? CurrentNativeView => currentNativeView; UIView? currentNativeView; + + // The handler needs this view before LoadView is called on the controller + // So this is used to create the first view that the handler will use + // without forcing the VC to call LoadView + UIView? _pendingLoadedView; + void SetView(IView? view, bool forceRefresh = false) { if (view == _view && !forceRefresh) @@ -28,26 +37,36 @@ void SetView(IView? view, bool forceRefresh = false) } currentNativeView?.RemoveFromSuperview(); currentNativeView = null; - if (IsViewLoaded) - { - LoadNativeView(); - } + if (IsViewLoaded && _view != null) + LoadNativeView(_view); + } + + internal UIView LoadFirstView(IView view) + { + _pendingLoadedView = CreateNativeView(view); + return _pendingLoadedView; } public override void LoadView() { base.LoadView(); if (_view != null && Context != null) - LoadNativeView(); + LoadNativeView(_view); } - internal void LoadNativeView() + void LoadNativeView(IView view) { - if (_view == null) - return; + currentNativeView = _pendingLoadedView ?? CreateNativeView(view); + _pendingLoadedView = null; + View!.AddSubview(currentNativeView); + } + protected virtual UIView CreateNativeView(IView view) + { _ = Context ?? throw new ArgumentNullException(nameof(Context)); - View!.AddSubview(currentNativeView = _view.ToNative(Context)); + _ = _view ?? throw new ArgumentNullException(nameof(view)); + + return _view.ToNative(Context); } public override void ViewDidLayoutSubviews() diff --git a/src/Core/src/Platform/iOS/LayoutView.cs b/src/Core/src/Platform/iOS/LayoutView.cs index 6b4bf4795a87..de72af51474b 100644 --- a/src/Core/src/Platform/iOS/LayoutView.cs +++ b/src/Core/src/Platform/iOS/LayoutView.cs @@ -60,13 +60,19 @@ public override void LayoutSubviews() public class PageViewController : ContainerViewController { - readonly IPage _page; - public PageViewController(IPage page,IMauiContext mauiContext) { - _page = page; CurrentView = page; Context = mauiContext; + LoadFirstView(page); + } + + protected override UIView CreateNativeView(IView view) + { + return new PageView + { + CrossPlatformArrange = view.Arrange, + }; } }