Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ protected override void Dispose(bool disposing)

protected override bool IsHorizontal => (ItemsView?.ItemsLayout as ItemsLayout)?.Orientation == ItemsLayoutOrientation.Horizontal;

public override nint NumberOfSections(UICollectionView collectionView)
{
var count = base.NumberOfSections(collectionView);

// UICollectionViewCompositionalLayout does not render global boundary supplementary items
// (header/footer set on NSCollectionLayoutConfiguration) when there are 0 sections.
// Return at least 1 section so the header/footer remains visible when ItemsSource is empty.
// Only applies to non-grouped collections: grouped collections put group headers in
// per-section supplementary items, which would crash when accessing the empty group source.
if (count == 0
&& ItemsSource is not null && !(ItemsView is GroupableItemsView { IsGrouped: true })
&& (ItemsView?.Header is not null || ItemsView?.HeaderTemplate is not null
|| ItemsView?.Footer is not null || ItemsView?.FooterTemplate is not null))
{
return 1;
}

return count;
}

internal void UpdateHeaderView()
{
// Clean up header view if no header content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ override Microsoft.Maui.Controls.Shapes.Shape.OnPropertyChanged(string? property
override Microsoft.Maui.Controls.Platform.Compatibility.ShellTableViewController.LoadView() -> void
*REMOVED*~override Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.TraitCollectionDidChange(UIKit.UITraitCollection previousTraitCollection) -> void
~override Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2.DisconnectHandler(UIKit.UIView platformView) -> void
~override Microsoft.Maui.Controls.Handlers.Items2.StructuredItemsViewController2<TItemsView>.NumberOfSections(UIKit.UICollectionView collectionView) -> nint
override Microsoft.Maui.Controls.Platform.Compatibility.ShellItemRenderer.ViewDidAppear(bool animated) -> void
~override Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.DidMoveToParentViewController(UIKit.UIViewController parent) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ override Microsoft.Maui.Controls.Shapes.Shape.OnPropertyChanged(string? property
override Microsoft.Maui.Controls.Platform.Compatibility.ShellTableViewController.LoadView() -> void
*REMOVED*~override Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.TraitCollectionDidChange(UIKit.UITraitCollection previousTraitCollection) -> void
~override Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2.DisconnectHandler(UIKit.UIView platformView) -> void
~override Microsoft.Maui.Controls.Handlers.Items2.StructuredItemsViewController2<TItemsView>.NumberOfSections(UIKit.UICollectionView collectionView) -> nint
override Microsoft.Maui.Controls.Platform.Compatibility.ShellItemRenderer.ViewDidAppear(bool animated) -> void
~override Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.DidMoveToParentViewController(UIKit.UIViewController parent) -> void
54 changes: 54 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue34897.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Microsoft.Maui.Controls;
using System.Collections.ObjectModel;
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.Collections.ObjectModel is not used in this file. Please remove the unused using to avoid IDE0005/style-analyzer noise in builds.

Suggested change
using System.Collections.ObjectModel;

Copilot uses AI. Check for mistakes.

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 34897, "CollectionView Header is not visible when ItemsSource is not set and EmptyView is set", PlatformAffected.iOS | PlatformAffected.macOS)]
public class Issue34897 : ContentPage
{
public Issue34897()
{
var collectionView = new CollectionView
{
ItemsLayout = new GridItemsLayout(2, ItemsLayoutOrientation.Vertical),
AutomationId = "Issue34897CollectionView",
Header = new Label
{
Text = "CollectionView Header",
AutomationId = "Issue34897Header",
BackgroundColor = Colors.LightBlue,
},
EmptyView = new Label
{
Text = "No items available",
AutomationId = "Issue34897EmptyView",
BackgroundColor = Colors.LightYellow,
},
Footer = new Label
{
Text = "CollectionView Footer",
AutomationId = "Issue34897Footer",
BackgroundColor = Colors.LightGreen,
},
};

var grid = new Grid
{
RowDefinitions =
{
new RowDefinition(GridLength.Auto),
new RowDefinition(GridLength.Star),
}
};

var label = new Label
{
Text = "Header, Footer and EmptyView should all be visible below:",
};

grid.Add(label, 0, 0);
grid.Add(collectionView, 0, 1);

Content = grid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#if TEST_FAILS_ON_WINDOWS // This test fails on Windows because we no longer works with CollectionView on Windows.
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file-level comment is grammatically incorrect and misleading (CollectionView still exists on Windows). Please update it to accurately describe why this test is excluded on Windows (e.g., EmptyView/header/footer elements not being accessible via Automation on Windows) and ideally reference the tracking issue if there is one.

Suggested change
#if TEST_FAILS_ON_WINDOWS // This test fails on Windows because we no longer works with CollectionView on Windows.
#if TEST_FAILS_ON_WINDOWS // Excluded on Windows because the CollectionView EmptyView, header, and footer are not reliably accessible via UI Automation there.

Copilot uses AI. Check for mistakes.
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue34897 : _IssuesUITest
{
public Issue34897(TestDevice device) : base(device) { }

public override string Issue => "CollectionView Header is not visible when ItemsSource is not set and EmptyView is set";

[Test]
[Category(UITestCategories.CollectionView)]
public void CollectionViewHeaderVisibleWithEmptyViewAndNullItemsSource()
{
App.WaitForElement("Issue34897EmptyView");
App.WaitForElement("Issue34897Header");
App.WaitForElement("Issue34897Footer");
}
}
#endif
Loading