Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Fixed HeaderFooterGrid to pass on CV1 and CV2 #26022

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -15,6 +15,8 @@ public abstract class ItemsViewController<TItemsView> : UICollectionViewControll
where TItemsView : ItemsView
{
public const int EmptyTag = 333;
const int HeaderTag = 111;
const int FooterTag = 222;
readonly WeakReference<TItemsView> _itemsView;

public IItemsViewSource ItemsSource { get; protected set; }
Expand Down Expand Up @@ -272,7 +274,37 @@ void InvalidateMeasureIfContentSizeChanged()
return _emptyUIView.Frame.Size.ToSize();
}

return CollectionView.CollectionViewLayout.CollectionViewContentSize.ToSize();
CGSize contentSize = CollectionView.CollectionViewLayout.CollectionViewContentSize;
CGSize headerSize = GetHeaderSize();
CGSize footerSize = GetFooterSize();

nfloat totalWidth = contentSize.Width;
nfloat totalHeight = contentSize.Height;

// Ensure calculating the total width or height, consider the boundary conditions to ensure it does not exceed the boundaries of the view.
// If it exceeds, the content becomes non-scrollable and is constrained to the view's screen size.
if (IsHorizontal)
{
totalWidth += headerSize.Width + footerSize.Width;
totalWidth = (nfloat)Math.Min(totalWidth, CollectionView.Bounds.Width);
}
else
{
totalHeight += headerSize.Height + footerSize.Height;
totalHeight = (nfloat)Math.Min(totalHeight, CollectionView.Bounds.Height);
}

return new Size(totalWidth, totalHeight);
}

CGSize GetHeaderSize()
{
return CollectionView.ViewWithTag(HeaderTag)?.Frame.Size ?? CGSize.Empty;
}

CGSize GetFooterSize()
{
return CollectionView.ViewWithTag(FooterTag)?.Frame.Size ?? CGSize.Empty;
}

void ConstrainItemsToBounds()
Expand Down
7 changes: 7 additions & 0 deletions src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ internal void SetInitialConstraints(CGSize size)
public virtual UIEdgeInsets GetInsetForSection(UICollectionView collectionView, UICollectionViewLayout layout,
nint section)
{
// If we're at the last section, we don't need to add the right inset
// Github issue : https://github.com/dotnet/maui/issues/25433
if (section >= (collectionView.NumberOfSections() - 1))
{
return UIEdgeInsets.Zero;
}

if (_itemsLayout is GridItemsLayout gridItemsLayout)
{
if (ScrollDirection == UICollectionViewScrollDirection.Horizontal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@
<Button Text="Toggle Header" Clicked="ToggleHeader"></Button>
<Button Text="Toggle Footer" Clicked="ToggleFooter"></Button>
</StackLayout>
<local:CollectionView2 x:Name="CollectionView" Grid.Row="1" >
<local:CollectionView2.ItemsLayout>
<CollectionView x:Name="CollectionView" Grid.Row="1" >
<CollectionView.ItemsLayout>
<GridItemsLayout Span="3" Orientation="Vertical" HorizontalItemSpacing="4" VerticalItemSpacing="2"></GridItemsLayout>
</local:CollectionView2.ItemsLayout>
<local:CollectionView2.Header>
</CollectionView.ItemsLayout>
<CollectionView.Header>
<StackLayout BackgroundColor="Transparent">
<Image Source="oasis.jpg" Aspect="AspectFill" HeightRequest="60"></Image>
<Label Text="This Is A Header" TextColor="AntiqueWhite" HorizontalTextAlignment="Center"
FontAttributes="Bold" FontSize="36" />
<Button Text="Add Content" Clicked="AddContentClicked"></Button>
</StackLayout>
</local:CollectionView2.Header>
<local:CollectionView2.Footer>
</CollectionView.Header>
<CollectionView.Footer>
<StackLayout BackgroundColor="Transparent">
<Image Source="oasis.jpg" Aspect="AspectFill" HeightRequest="80"></Image>
<Label Text="This Is A Footer" TextColor="AntiqueWhite" HorizontalTextAlignment="Center" Rotation="10"
FontAttributes="Bold" FontSize="20" />
<Button Text="Add Content" Clicked="AddContentClicked"></Button>
</StackLayout>
</local:CollectionView2.Footer>
</local:CollectionView2>
</CollectionView.Footer>
</CollectionView>
</Grid>
</ContentPage.Content>
</ContentPage>