Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,65 @@ 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;

var internalScrollView = FindInternalScrollView(nativeCollectionView);
if (internalScrollView != null)
{
Assert.True(internalScrollView.ShowsHorizontalScrollIndicator == true);
Assert.True(internalScrollView.ShowsVerticalScrollIndicator == true);
}

Comment thread
SubhikshaSf4851 marked this conversation as resolved.
Outdated
// Test ScrollBarVisibility.Never
carouselView.HorizontalScrollBarVisibility = ScrollBarVisibility.Never;
carouselView.VerticalScrollBarVisibility = ScrollBarVisibility.Never;

Assert.True(nativeCollectionView.ShowsHorizontalScrollIndicator == false);
Assert.True(internalScrollView.ShowsVerticalScrollIndicator == false);
Comment thread
SubhikshaSf4851 marked this conversation as resolved.
Outdated
Comment thread
SubhikshaSf4851 marked this conversation as resolved.
Outdated
});
}

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;
}
}
}
47 changes: 47 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,58 @@ 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;
}

// Internal scroll view may not be created yet, so retry on the main thread after layout.
collectionView.BeginInvokeOnMainThread(() =>
{
TryApplyToInternalScrollView(collectionView, isVertical);
});
}

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