Programatically scroll in CollectionView iOS [XAMARIN]

Ferenc Dajka 21 Reputation points
2022-03-29T18:44:18.353+00:00

So on Android I have a custom Renderer, for the View. How can I achieve the same on iOS?

[assembly: ExportRenderer(typeof(CustomCollectionView), typeof(CustomCollectionViewRenderer))]
namespace AppTourism.Droid.Renderers
{
    public class CustomCollectionViewRenderer : CollectionViewRenderer, ICustomCollectionViewRenderer
    {

        public void Scroll(int x, int y)
        {
            ScrollBy(x, y);
        }
    }

}
Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2022-03-30T09:06:40.957+00:00

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.