Skip to content

Commit

Permalink
[iOS] Shell toolbar items (#20510)
Browse files Browse the repository at this point in the history
* Shell toolbar items fix

* Added snapshots
  • Loading branch information
kubaflo authored Dec 20, 2024
1 parent d57502f commit 01474fb
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,25 @@ protected virtual void UpdateToolbarItems()
NavigationItem.RightBarButtonItems[i].Dispose();
}

var shellToolbarItems = _context?.Shell?.ToolbarItems;
List<UIBarButtonItem> primaries = null;
if (Page.ToolbarItems.Count > 0)
if (Page.ToolbarItems.Count > 0) // Display toolbar items defined on the current page
{
foreach (var item in System.Linq.Enumerable.OrderBy(Page.ToolbarItems, x => x.Priority))
{
(primaries = primaries ?? new List<UIBarButtonItem>()).Add(item.ToUIBarButtonItem(false, true));
}

if (primaries != null)
primaries.Reverse();
}
else if (shellToolbarItems != null && shellToolbarItems.Count > 0) // If the page has no toolbar items use the ones defined for the shell
{
foreach (var item in System.Linq.Enumerable.OrderBy(shellToolbarItems, x => x.Priority))
{
(primaries = primaries ?? new List<UIBarButtonItem>()).Add(item.ToUIBarButtonItem(false, true));
}
}

if (primaries != null)
primaries.Reverse();

NavigationItem.SetRightBarButtonItems(primaries == null ? Array.Empty<UIBarButtonItem>() : primaries.ToArray(), false);

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue18946.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue18946">
<Shell.TitleView>
<HorizontalStackLayout Margin="10" HorizontalOptions="Center" VerticalOptions="Fill" >
<Image AutomationId="image" Source="small_dotnet_bot.png" HeightRequest="48" WidthRequest="48" />
<Label VerticalOptions="Center" VerticalTextAlignment="Center" Text="Maui App" FontSize="20" />
</HorizontalStackLayout>
</Shell.TitleView>

<Shell.ToolbarItems>
<ToolbarItem Text="Hello"/>
</Shell.ToolbarItems>

<ShellContent Title="Home" Route="MainPage">
<ContentPage>
<Label Text="Hello, MAUI!" AutomationId="label"/>
</ContentPage>
</ShellContent>
</Shell>
16 changes: 16 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue18946.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 18946, "Shell Toolbar items not displayed", PlatformAffected.All)]
public partial class Issue18946 : Shell
{
public Issue18946()
{
InitializeComponent();
}
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue18946 : _IssuesUITest
{
public override string Issue => "Shell Toolbar items not displayed";

public Issue18946(TestDevice device) : base(device)
{ }

[Test]
[Category(UITestCategories.Shell)]
public void ToolbarItemsShouldWork()
{
_ = App.WaitForElement("label");

// The test passes if the text 'hello' is visible in the toolbar
VerifyScreenshot();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 01474fb

Please sign in to comment.