Skip to content

Commit dd92b66

Browse files
[iOS/Mac] Fixed OnBackButtonPressed not firing for Shell Navigation Bar Button (#34401)
<!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details The OnBackButtonPressed override is not triggered when the back button in the Shell Navigation Bar is clicked. ### Root Cause The ShouldPopItem method (called by iOS navigation bar when the back button is tapped) calls SendPop(), which only checks for BackButtonBehavior.Command but never calls Page.SendBackButtonPressed(). ### Description of Change Added SendBackButtonPressed() check inside the tracker loop in SendPop(), after the existing BackButtonBehavior.Command check, before the break. ### Validated the behaviour in the following platforms - [ ] Android - [ ] Windows - [x] iOS - [x] Mac ### Issues Fixed: Fixes #34190 ### Screenshots | Before  | After | |---------|--------| |  <video src="https://github.com/user-attachments/assets/6987afb2-8a0d-4bb0-aa49-8f5b53931bae"> |   <video src="https://github.com/user-attachments/assets/a32ed322-7ec4-44f4-94de-dc56c191a79f">  |
1 parent 23a7503 commit dd92b66

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellSectionRenderer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ internal bool SendPop(UIViewController topViewController = null)
144144
return false;
145145
}
146146

147+
// Allow the page to intercept back navigation via OnBackButtonPressed
148+
if (tracker.Value.Page?.SendBackButtonPressed() == true)
149+
{
150+
return false;
151+
}
152+
147153
break;
148154
}
149155
}

src/Controls/tests/TestCases.HostApp/Issues/Issue33523.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Maui.Controls.Sample.Issues
44
{
5-
[Issue(IssueTracker.Github, 33523, "OnBackButtonPressed not firing for Shell Navigation Bar button in .NET 10 SR2", PlatformAffected.Android)]
5+
[Issue(IssueTracker.Github, 33523, "OnBackButtonPressed not firing for Shell Navigation Bar button in .NET 10 SR2", PlatformAffected.Android | PlatformAffected.iOS | PlatformAffected.MacCatalyst)]
66
public class Issue33523 : Shell
77
{
88
public Issue33523()

src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue33523.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#if TEST_FAILS_ON_CATALYST && TEST_FAILS_ON_IOS // Issue: https://github.com/dotnet/maui/issues/34190
2-
31
using NUnit.Framework;
42
using UITest.Appium;
53
using UITest.Core;
@@ -26,19 +24,20 @@ public void OnBackButtonPressedShouldFireForShellNavigationBarButton()
2624
// Verify initial state
2725
var statusLabel = App.WaitForElement("StatusLabel");
2826
Assert.That(statusLabel.GetText(), Is.EqualTo("OnBackButtonPressed not called"));
29-
30-
// Tap the navigation bar back button
31-
// Note: This uses the Shell's navigation bar back button, not the system back button
32-
App.TapBackArrow();
33-
34-
// Wait a moment for the event to fire
27+
if (App is AppiumIOSApp iosApp && HelperExtensions.IsIOS26OrHigher(iosApp))
28+
{
29+
App.TapBackArrow(); // In iOS 26, the previous page title is not shown along with the back arrow, so we use the default back arrow
30+
}
31+
else
32+
{
33+
App.TapBackArrow(Device is TestDevice.iOS or TestDevice.Mac ? "Main Page" : "");
34+
}
3535
App.WaitForElement("StatusLabel");
3636

3737
// Verify OnBackButtonPressed was called
3838
statusLabel = App.FindElement("StatusLabel");
39-
Assert.That(statusLabel.GetText(), Is.EqualTo("OnBackButtonPressed was called"),
39+
Assert.That(statusLabel.GetText(), Is.EqualTo("OnBackButtonPressed was called"),
4040
"OnBackButtonPressed should be called when tapping the Shell Navigation Bar back button");
4141
}
4242
}
4343
}
44-
#endif

0 commit comments

Comments
 (0)