Skip to content
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 @@ -319,5 +319,77 @@ Rect GetCollectionViewCellBounds(IView cellContent)

return cellContent.ToPlatform().GetParentOfType<UIKit.UICollectionViewCell>().GetBoundingBox();
}

[Fact(DisplayName = "CarouselView ScrollBar Visibility should Update")]
public async Task CheckCarouselViewScrollBarVisibilityUpdates()
{
EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<CarouselView, CarouselViewHandler2>();
handlers.AddHandler<Label, LabelHandler>();
});
});

var carouselView = new CarouselView
{
ItemsSource = new List<string> { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" },
ItemTemplate = new DataTemplate(() => new Label { WidthRequest = 200 })
};

await CreateHandlerAndAddToWindow<CarouselViewHandler2>(carouselView, async handler =>
{
await Task.Delay(100); // Allow layout to complete
var nativeCollectionView = handler.Controller?.CollectionView;
Assert.NotNull(nativeCollectionView);

// CarouselView should use CompositionalLayout
Assert.IsType<UICollectionViewCompositionalLayout>(nativeCollectionView.CollectionViewLayout);

// Test ScrollBarVisibility.Always
carouselView.HorizontalScrollBarVisibility = ScrollBarVisibility.Always;
carouselView.VerticalScrollBarVisibility = ScrollBarVisibility.Always;
await Task.Delay(100); // Allow BeginInvokeOnMainThread callbacks to drain

// Poll for the internal scroll view (it may not be created synchronously)
UIScrollView internalScrollView = null;
for (int attempt = 0; attempt < 10 && internalScrollView == null; attempt++)
{
internalScrollView = FindInternalScrollView(nativeCollectionView);
if (internalScrollView == null)
await Task.Delay(100);
}
Assert.NotNull(internalScrollView); // Must exist for the test to be valid

Assert.True(internalScrollView.ShowsHorizontalScrollIndicator);
Assert.True(internalScrollView.ShowsVerticalScrollIndicator);
Assert.True(nativeCollectionView.ShowsHorizontalScrollIndicator);
Assert.True(nativeCollectionView.ShowsVerticalScrollIndicator);

// Test ScrollBarVisibility.Never
carouselView.HorizontalScrollBarVisibility = ScrollBarVisibility.Never;
carouselView.VerticalScrollBarVisibility = ScrollBarVisibility.Never;
await Task.Delay(100);

Assert.False(internalScrollView.ShowsHorizontalScrollIndicator); // Key assertion for this bug!
Assert.False(internalScrollView.ShowsVerticalScrollIndicator);
Assert.False(nativeCollectionView.ShowsHorizontalScrollIndicator);
Assert.False(nativeCollectionView.ShowsVerticalScrollIndicator);
});
}

private static UIScrollView FindInternalScrollView(UICollectionView collectionView)
{
// In CV2 the scroll indicators are managed by an internal UIScrollView.
foreach (var subview in collectionView.Subviews)
{
if (subview is UIScrollView scrollView && scrollView != collectionView)
{
return scrollView;
}
}
return null;
}
}
}
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.
52 changes: 52 additions & 0 deletions src/Core/src/Platform/iOS/CollectionViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,63 @@ public static class CollectionViewExtensions
public static void UpdateVerticalScrollBarVisibility(this UICollectionView collectionView, ScrollBarVisibility scrollBarVisibility)
{
collectionView.ShowsVerticalScrollIndicator = scrollBarVisibility == ScrollBarVisibility.Always || scrollBarVisibility == ScrollBarVisibility.Default;

// In CV2 the scroll indicators are managed by an internal UIScrollView.
if (collectionView.CollectionViewLayout is UICollectionViewCompositionalLayout)
{
UpdateInternalScrollView(collectionView, true);
}
}

public static void UpdateHorizontalScrollBarVisibility(this UICollectionView collectionView, ScrollBarVisibility scrollBarVisibility)
{
collectionView.ShowsHorizontalScrollIndicator = scrollBarVisibility == ScrollBarVisibility.Always || scrollBarVisibility == ScrollBarVisibility.Default;

// In CV2 the scroll indicators are managed by an internal UIScrollView.
if (collectionView.CollectionViewLayout is UICollectionViewCompositionalLayout)
{
UpdateInternalScrollView(collectionView, false);
}
}

static void UpdateInternalScrollView(UICollectionView collectionView, bool isVertical)
{
if (TryApplyToInternalScrollView(collectionView, isVertical))
{
return;
}

// One deferred retry only; if the scroll view still doesn't exist at that point,
// the setting is not applied (acceptable: layout should always be complete by then).
collectionView.BeginInvokeOnMainThread(() =>
{
TryApplyToInternalScrollView(collectionView, isVertical);
});
}

// NOTE: This relies on UICollectionViewCompositionalLayout's internal implementation —
// it creates a UIScrollView subview to host scroll indicators. If this changes in a
// future iOS release, this method will return false and scrollbar visibility will fall
// back to the outer UICollectionView only.
static bool TryApplyToInternalScrollView(UICollectionView collectionView, bool isVertical)
{
foreach (var subview in collectionView.Subviews)
{
if (subview is UIScrollView scrollView && scrollView != collectionView)
{
if (isVertical)
{
scrollView.ShowsVerticalScrollIndicator = collectionView.ShowsVerticalScrollIndicator;
}
else
{
scrollView.ShowsHorizontalScrollIndicator = collectionView.ShowsHorizontalScrollIndicator;
}
return true;
}
}

return false;
}
}
}
Loading