Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,7 @@ public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);

TabbedPage.OnNavigationChildAppeared(Child, this);
Appearing?.Invoke(this, EventArgs.Empty);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,13 @@ void OnPropertyChanged(object sender, PropertyChangedEventArgs e)

SetNeedsUpdateOfHomeIndicatorAutoHidden();
SetNeedsStatusBarAppearanceUpdate();
SelectedViewController = controller;
if (Tabbed?.IsSelectingMorePage(current) != true &&
SelectedViewController != controller &&
!(SelectedViewController == MoreNavigationController &&
MoreNavigationController.TopViewController == controller))
{
SelectedViewController = controller;
}
}
else if (e.PropertyName == nameof(TabbedPage.FlowDirection))
UpdateFlowDirection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ public override void ViewWillAppear(bool animated)
base.ViewWillAppear(animated);
}

public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
TabbedPage.OnNavigationChildAppeared(Child, this);
}

public override void ViewWillLayoutSubviews()
{
base.ViewWillLayoutSubviews();
Expand Down
50 changes: 48 additions & 2 deletions src/Controls/src/Core/TabbedPage/TabbedPage.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Microsoft.Maui.Controls
{
public partial class TabbedPage
public partial class TabbedPage : IPageViewControllerAppearanceObserver
{
// Instance state for tracking tab bar defaults (used by appearance mappers)
bool _barBackgroundColorWasSet;
Expand All @@ -28,6 +28,7 @@ public partial class TabbedPage
Brush _currentBarBackground;
WeakBrushChangedProxy _barBackgroundProxy;
EventHandler _barBackgroundChanged;
Page _selectingMorePage;

// Tracks pages from the previous MapItemsSource call so we can
// disconnect handlers for pages that were removed.
Expand Down Expand Up @@ -703,7 +704,10 @@ internal static void MapCurrentPage(ITabbedViewHandler handler, TabbedPage view)
tabBarController?.SetNeedsUpdateOfHomeIndicatorAutoHidden();
tabBarController?.SetNeedsStatusBarAppearanceUpdate();

if (controller != manager.SelectedViewController)
if (!view.IsSelectingMorePage(current) &&
controller != manager.SelectedViewController &&
!(manager.SelectedViewController == manager.MoreNavigationController &&
controller == manager.MoreNavigationController.TopViewController))
{
manager.SelectedViewController = controller;
}
Expand Down Expand Up @@ -956,5 +960,47 @@ static void RefreshTabBarItemTags(TabbedPage view)
}
}
}

internal bool IsSelectingMorePage(Page page) => _selectingMorePage == page;

void IPageViewControllerAppearanceObserver.OnPageViewControllerAppeared(IElement page, UIViewController controller)
{
if (page is Page tab && GetViewController(tab) == controller)
SyncMoreSelection(tab, controller);
}

internal static void OnNavigationChildAppeared(Page child, UIViewController controller)
{
if (child?.RealParent is NavigationPage navigationPage && navigationPage.RealParent is TabbedPage tabbedPage)
tabbedPage.SyncMoreSelection(navigationPage, controller);
}

void SyncMoreSelection(Page tab, UIViewController appeared)
{
if (CurrentPage == tab || !Children.Contains(tab) ||
Handler is not IPlatformViewHandler { ViewController: UITabBarController tabBar } ||
GetViewController(tab) is not UIViewController tabController)
return;

var more = tabBar.MoreNavigationController;
var selected = tabBar.SelectedViewController;
var top = more.TopViewController;
// UIKit can select the tab's controller while displaying its child controller in More.
if ((selected != more && selected != tabController) ||
(top != appeared && !(top == tabController &&
Comment thread
github-actions[bot] marked this conversation as resolved.
Resolved
tabController is UINavigationController stack && stack.TopViewController == appeared)))
return;

var previousSelection = _selectingMorePage;
_selectingMorePage = tab;
try
{
CurrentPage = tab;
}
finally
{
_selectingMorePage = previousSelection;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using CoreAnimation;
using Foundation;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Platform;
using ObjCRuntime;
using UIKit;
using Xunit;
using static Microsoft.Maui.DeviceTests.AssertHelpers;
Expand All @@ -20,6 +23,147 @@ namespace Microsoft.Maui.DeviceTests
[Category(TestCategory.TabbedPage)]
public partial class TabbedPageTests
{
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task SelectingNativeMoreRowUpdatesCurrentPage(bool useNavigationPage)
{
if (UIDevice.CurrentDevice.UserInterfaceIdiom != UIUserInterfaceIdiom.Phone)
return;

SetupBuilder();
var content = new ContentPage { Title = "Overflow" };
Page overflow = useNavigationPage ? new NavigationPage(content) { Title = "Overflow" } : content;
var pages = Enumerable.Range(1, 5).Select(i => (Page)new ContentPage { Title = $"Tab {i}" }).Append(overflow);
var tabbedPage = CreateBasicTabbedPage(pages: pages);
var navigatedTo = 0;
overflow.NavigatedTo += (_, _) => navigatedTo++;

await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(tabbedPage), async _ =>
{
await OnLoadedAsync(tabbedPage.Children[0]);
var tabBar = Assert.IsAssignableFrom<UITabBarController>(((IPlatformViewHandler)tabbedPage.Handler).ViewController);
var more = tabBar.MoreNavigationController;
tabBar.SelectedViewController = more;
await AssertEventually(() => more.TopViewController?.View is UITableView { Window: not null });
var table = Assert.IsAssignableFrom<UITableView>(more.TopViewController.View);

var selectionDelegate = Assert.IsAssignableFrom<NSObject>(table.WeakDelegate);
using var row = NSIndexPath.FromRowSection(1, 0);
SelectNativeMoreRow(selectionDelegate.Handle, Selector.GetHandle("tableView:didSelectRowAtIndexPath:"), table.Handle, row.Handle);

await AssertEventually(() => tabbedPage.CurrentPage == overflow);
Assert.Equal(1, navigatedTo);
});
}

[DllImport(Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSend")]
static extern void SelectNativeMoreRow(NativeHandle receiver, IntPtr selector, NativeHandle table, NativeHandle row);

[Fact]
public async Task InactiveMorePageAppearanceDoesNotChangeCurrentPage()
{
if (UIDevice.CurrentDevice.UserInterfaceIdiom != UIUserInterfaceIdiom.Phone)
return;

SetupBuilder();
await InvokeOnMainThreadAsync(() =>
{
var overflow = new ContentPage { Title = "Overflow" };
var pages = Enumerable.Range(1, 6).Select(i => (Page)new ContentPage { Title = $"Tab {i}" }).Append(overflow);
var tabbedPage = CreateBasicTabbedPage(pages: pages);
var handler = Assert.IsAssignableFrom<IPlatformViewHandler>(tabbedPage.ToHandler(MauiContext));
var tabBar = Assert.IsAssignableFrom<UITabBarController>(handler.ViewController);
var originalControllers = tabBar.ViewControllers;
UINavigationController more = null;
PageViewController overflowController = null;
try
{
var activePage = tabbedPage.Children[0];
var activeController = Assert.IsAssignableFrom<IPlatformViewHandler>(activePage.Handler).ViewController;
overflowController = Assert.IsAssignableFrom<PageViewController>(
Assert.IsAssignableFrom<IPlatformViewHandler>(overflow.Handler).ViewController);
Assert.Same(overflow, overflowController.CurrentView);
Assert.Same(tabbedPage, ((IElement)overflow).Parent);

// Keep More available, but remove the overflow controller from the native tab list
// before putting it on the More stack. The MAUI page remains a child of TabbedPage.
tabBar.ViewControllers = originalControllers.Where(controller => controller != overflowController).ToArray();
tabBar.SelectedViewController = activeController;
more = tabBar.MoreNavigationController;
more.PushViewController(overflowController, false);

Assert.Same(activePage, tabbedPage.CurrentPage);
Assert.Same(activeController, tabBar.SelectedViewController);
Assert.Same(overflowController, more.TopViewController);

var navigatingFrom = 0;
var navigatedFrom = 0;
var navigatedTo = 0;
activePage.NavigatingFrom += (_, _) => navigatingFrom++;
activePage.NavigatedFrom += (_, _) => navigatedFrom++;
overflow.NavigatedTo += (_, _) => navigatedTo++;

overflowController.ViewDidAppear(false);

Assert.Same(activePage, tabbedPage.CurrentPage);
Assert.Same(activeController, tabBar.SelectedViewController);
Assert.Equal(0, navigatingFrom);
Assert.Equal(0, navigatedFrom);
Assert.Equal(0, navigatedTo);
}
finally
{
if (more is not null && more.TopViewController == overflowController)
more.PopViewController(false);
tabBar.ViewControllers = originalControllers;
tabbedPage.Handler = null;
if (handler is TabbedRenderer renderer)
renderer.Dispose();
}
});
}

[Fact]
public async Task ReattachingTabbedPageLeavesMoreNavigationDelegatesUntouched()
{
SetupBuilder();
await InvokeOnMainThreadAsync(() =>
{
var tabbedPage = CreateBasicTabbedPage();
var firstHandler = Assert.IsAssignableFrom<IPlatformViewHandler>(tabbedPage.ToHandler(MauiContext));
var firstController = Assert.IsAssignableFrom<UITabBarController>(firstHandler.ViewController);
var firstMore = firstController.MoreNavigationController;
using var firstDelegate = new UINavigationControllerDelegate();
firstMore.Delegate = firstDelegate;

Detach(firstHandler);
Assert.Same(firstDelegate, firstMore.Delegate);

var secondHandler = Assert.IsAssignableFrom<IPlatformViewHandler>(tabbedPage.ToHandler(MauiContext));
var secondController = Assert.IsAssignableFrom<UITabBarController>(secondHandler.ViewController);
var secondMore = secondController.MoreNavigationController;
using var secondDelegate = new UINavigationControllerDelegate();
try
{
secondMore.Delegate = secondDelegate;
Assert.Same(firstDelegate, firstMore.Delegate);
}
finally
{
Detach(secondHandler);
}
Assert.Same(secondDelegate, secondMore.Delegate);

void Detach(IPlatformViewHandler handler)
{
tabbedPage.Handler = null;
if (handler is TabbedRenderer renderer)
renderer.Dispose();
}
});
}

[Fact(DisplayName = "TabbedPage BarBackground gradient persists after iOS 15 appearance update")]
public async Task BarBackgroundGradientPersistsAfteriOS15AppearanceUpdate()
{
Expand Down
Loading
Loading