-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
The width of CollectionView's footer and header is not correct #17885
Comments
We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process. |
Verified this on Visual Studio Enterprise 17.8.0 Preview 3.0(8.0.0-rc.2.9373). Repro on iOS 16.4, not repro on Android 13.0-API33 with provided Project: |
This bug also impacts any CollectionView header that uses CSS as well (on iOS). The header always stays the initial size, and the content displays out of the bounds, causing the elements to overlap the CollectionView items. This bug causes problems with both width/height. I created a simple behavior that seems to fix the issue for me: public class CollectionViewResizeHeaderBehavior : Behavior<CollectionView>
{
protected override void OnAttachedTo( CollectionView bindable )
{
/*
* BC 10/17/2023
* This is some iOS specific code that is needed to force the header to resize when the inner content is resized.
* This is a bug in .NET MAUI, and we can track the progress here: https://github.com/dotnet/maui/issues/17885
*/
#if IOS
if ( bindable.Header is VisualElement element )
{
bindable.Dispatcher.Dispatch( () =>
{
element.InvalidateMeasureNonVirtual( ( Microsoft.Maui.Controls.Internals.InvalidationTrigger.Undefined ) );
} );
}
#endif
base.OnAttachedTo( bindable );
}
} In my case, I add the behavior to every CollectionView thru the handler: /// <summary>
/// Configures the Collection View handler to apply a behavior that fixes a bug in .NET MAUI where the
/// header of a collection view does not resize when the inner content.
/// </summary>
private static void ConfigureCollectionViewHeaderFix()
{
CollectionViewHandler.Mapper.AppendToMapping( "CollectionViewResizeHeaderBehavior", ( handler, view ) =>
{
if ( view is CollectionView collectionView )
{
collectionView.Behaviors.Add( new CollectionViewResizeHeaderBehavior() );
}
} );
} |
In fact, both Header and Footer have added MeasureInvalidated. I currently just add a property to manually trigger the MapHeaderTemplate or MapFooterTemplate when Header (Footer) needs
|
Not only the width, but also the height is completely fixed, which prevents me from dynamically displaying the view`s height based on the Binding. |
@maonaoda I am experiencing the same issue. From my testing the CollectionView Header dimensions do not update if elements are shown / hidden or the device orientation is changed. Also, if you constrain the width of the CollectionView the header does not constrain to that same value. Calling InvalidateMeasureNonVirtual(Microsoft.Maui.Controls.Internals.InvalidationTrigger.Undefined) after showing / hiding elements (and in OnSizeAllocated override for device orientation changes) does fix but is a hack. |
It needs a suitable time to use InvalidateMeasureNonVirtual(Microsoft.Maui.Controls.Internals.InvalidationTrigger.Undefined)
Set CollectionViewHeaderFooterLayoutHandler to CollectionViewHeaderFooterGrid and CollectionViewHeaderFooterStackLayout ,then use CollectionViewHeaderFooterGrid and CollectionViewHeaderFooterStackLayout under CollectionView.Header or CollectionView.Footer ※Note that there may be a better way. But currently I can only use this because maui has no plans to repair it at all. |
@samhouts do you have some news about this? |
@maonaoda why you check heightConstraint - size.Height when the problem is on the width? |
Description
The ViewWillLayoutSubviews() method will be executed multiple times and CollectionView.Frame.Width is finally set to the correct value.
But UpdateHeaderFooterPosition() will not be executed again at the finally execution,
so header and footer`s width will set to the wrong CollectionView.Frame.Width.
As the comments in the code say, ViewWillLayoutSubviews() only recalculates the footer`s position, but actually changes the width of the header and footer, so it is necessary to fix it.
https://github.com/dotnet/maui/blob/ff50be2f541a7eef49243c4d23c4615db20fc581/src/Controls/src/Core/Handlers/Items/iOS/StructuredItemsViewController.cs#L78C48-L78C48
https://github.com/dotnet/maui/blob/ff50be2f541a7eef49243c4d23c4615db20fc581/src/Controls/src/Core/Handlers/Items/iOS/StructuredItemsViewController.cs#L149C3-L149C36
Steps to Reproduce
No response
Link to public reproduction project repository
No response
Version with bug
8.0.0-rc.1.9171
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
iOS
Affected platform versions
No response
Did you find any workaround?
Maybe there is another better way.
Relevant log output
No response
The text was updated successfully, but these errors were encountered: