locked
iOS CollectionViewRenderer get UICollectionView RRS feed

  • Question

  • User385619 posted

    Hi all,

    I'm a bit lost, perhaps the day was just too long, I don't know.

    I want to write a custom render for Xamarin CollectionView https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/ so I implemented a Renderer extending from CollectionViewRenderer. The goal is to have a Reorderable Drag and drop ListView.

    This worked fine for Android which was really cool. Under iOS however, if I try to get the Control in the Renderer as UICollectionView or as UIVollectionViewController it is not possible, as it seems the type of Control is UICollectionViewControllerWrapperView. But this class on the other hand doesn't seem to exist in order to cast to it.

    I understand that the CollectionView is still in Preview but I guess that it is possible already to write simple custom renderers. According to documentations the Control of CollectionView should be an UICollectionView so in the renderer it should be possible to access it, no? So basically if I come to this part:

    if(Control is UICollectionView collectionView)
    {
        ///Some stuff here 
    }
    

    the if is skipped because Control is not of UICollectionView.

    Who knows how to get the UICollectionView in order to apply the delegate for drag and drop?

    Thank you already.

    Monday, May 27, 2019 3:54 PM

Answers

  • User369978 posted

    According to the official documentation , the base control on iOS platform for CollectionViewRenderer is UICollectionView ,i suggest you file issue on github :https://github.com/xamarin/Xamarin.Forms/issues

    However i got the UICollectionView by the following code

    class MyRenderer : CollectionViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<CollectionView> e)
        {
            base.OnElementChanged(e);
    
            if(Control != null)
            {
                NSArray s = Control.ValueForKey(new NSString("_subviewCache")) as NSMutableArray;
                UICollectionView c = s.GetItem<UICollectionView>(0);
            }
        }
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 28, 2019 2:29 AM
  • User385619 posted

    Thank you @ColeX I will file the issue on GitHub then. Thanks for your solution I will give it a try.

    In the meantime I also managed to get the UICollectionView by the following code:

    protected override void OnElementChanged(ElementChangedEventArgs<ItemsView> e)
    {
                base.OnElementChanged(e);
                //This works but shouldnt be the supossed way of getting it!
                if(Control.PreferredFocusEnvironments[0] is UICollectionView collectionView)
                {
                    //Do stuff
                }
    }
    

    But both our solutions shouldn't be the way to go :)

    Thank you.

    Edit:

    Filed the issue here https://github.com/xamarin/Xamarin.Forms/issues/6325

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 28, 2019 5:51 AM

All replies

  • User369978 posted

    According to the official documentation , the base control on iOS platform for CollectionViewRenderer is UICollectionView ,i suggest you file issue on github :https://github.com/xamarin/Xamarin.Forms/issues

    However i got the UICollectionView by the following code

    class MyRenderer : CollectionViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<CollectionView> e)
        {
            base.OnElementChanged(e);
    
            if(Control != null)
            {
                NSArray s = Control.ValueForKey(new NSString("_subviewCache")) as NSMutableArray;
                UICollectionView c = s.GetItem<UICollectionView>(0);
            }
        }
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 28, 2019 2:29 AM
  • User385619 posted

    Thank you @ColeX I will file the issue on GitHub then. Thanks for your solution I will give it a try.

    In the meantime I also managed to get the UICollectionView by the following code:

    protected override void OnElementChanged(ElementChangedEventArgs<ItemsView> e)
    {
                base.OnElementChanged(e);
                //This works but shouldnt be the supossed way of getting it!
                if(Control.PreferredFocusEnvironments[0] is UICollectionView collectionView)
                {
                    //Do stuff
                }
    }
    

    But both our solutions shouldn't be the way to go :)

    Thank you.

    Edit:

    Filed the issue here https://github.com/xamarin/Xamarin.Forms/issues/6325

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 28, 2019 5:51 AM
  • User330867 posted

    Why do some people say we have to inherit from ViewRenderer and in some other places I see CollectionViewRenderer?!

    Sunday, February 28, 2021 12:24 PM