Hello,
You could try to set ContentOffset of the collectionview in iOS, it means the offset for the origin of the content inside a scroll view.
public void Scroll(int x, int y)
{
SetContentOffset(new CGPoint(x, y), true);
}
You could also check the scrolling in Xamarin.Forms: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/scrolling#detect-scrolling
---------UPDATE----------
You need to find the native UICollectionView
:
public class CustomCollectionRenderer_iOS : CollectionViewRenderer
{
UICollectionView nativeCollectionView;
protected override void OnElementChanged(ElementChangedEventArgs<GroupableItemsView> e)
{
base.OnElementChanged(e);
if (Control != null)
{
if (Control.PreferredFocusEnvironments[0] is UICollectionView collectionView)
{
nativeCollectionView = Control.PreferredFocusEnvironments[0] as
UICollectionView;// find the native UICollectionView
}
}
UIButton btn = new UIButton();
btn.Frame = new CGRect(0, 80, 50, 50);
btn.BackgroundColor = UIColor.Red;
btn.TouchUpInside += (s, er) =>
{
Scroll(0, 200);
};
AddSubview(btn);// click the button to test
}
public void Scroll(int x, int y)
{
nativeCollectionView.SetContentOffset(new CGPoint(x, y), true);
}
}
There is a known issue with how iOS structures the classes around UICollectionView in GitHub, you could follow the progress. You could also try class CollectionViewRenderer : ViewRenderer<CollectionView, UICollectionView>
, but in this way, you have to set the native control and custom the UICollectionViewCell
and UICollectionViewFlowLayout
, refer to https://learn.microsoft.com/en-us/xamarin/ios/user-interface/controls/uicollectionview
if (Control == null) {
SetNativeControl (new UICollectionView (new CGRect (0, 0, 300, 256), new UICollectionViewFlowLayout ()));
Control.RegisterClassForCell(typeof(UICollectionViewCell), "MyCell");
}
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.