xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.64k stars 1.88k forks source link

[Bug] CollectionViewRenderer, Control under iOS not of type UICollectionView #6325

Open denzerd opened 5 years ago

denzerd commented 5 years ago

Description

According to the documentations https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/renderers#views Xamarin Forms' CollectionView should be of type UICollectionView under iOS. However when implementing a Customrenderer with the help of CollectionViewRenderer the Control is of type UICollectionViewControllerWrapperView. It is difficult or can just be done by tricks to get the actual UICollectionView to manipulate the rendering.

Steps to Reproduce

  1. Create a new Xamarin Forms app
  2. Create a class inheriting from CollectionView
  3. In the iOS project create a custom renderer inheriting from CollectionViewRenderer
  4. In the OnElementChanged (Or somewhere you want) print/see the type of Control

Expected Behavior

Control of an iOS CollectionViewRenderer is of type UICollectionView

Actual Behavior

Control of an iOSCollectionViewRenderer is of type UICollectionViewControllerWrapperView

Basic Information

Links

Had a question on the Xamarin community about it already with two workarounds: https://forums.xamarin.com/discussion/156810/ios-collectionviewrenderer-get-uicollectionview#latest

GalaxiaGuy commented 5 years ago

The ViewController property on CollectionViewRenderer is an ItemsViewController, which subclasses UICollectionViewControler. This might allow you to do what you want to do.

It seems actually getting hold of the real UICollectionView being difficult is just a "feature" of iOS:

https://stackoverflow.com/questions/18967928/uicollectionviewcontrollers-layout-ownership-self-collectionview-collectionvie

denzerd commented 5 years ago

@GalaxiaGuy Thanks this is at least an easier way to get the ViewController itself as the two solutions we found In the Xamarin Board.

However the issue persists as the documentation clearly states that the CollectionView is of type UICollectionView under iOS, so either the documentation has to be adjusted or there is something wrong with CollectionViewRenderer under iOS.

Thanks again, good solution for the moment.

hartez commented 5 years ago

Yeah, this is an issue with how iOS structures the classes around UICollectionView. If we actually set Control to be the UICollectionView itself, things get weird with layout; we have to go through the intermediary wrapper control.

I'll see what we can do about noting this in the docs, and possibly adding a more direct property for accessing the UICollectionView itself.

adrianknight89 commented 4 years ago

@denzerd There is currently no UICollectionViewControllerWrapperView in the source code as far as I can see. You could currently get the UICollectionView from ItemsViewController.CollectionView property in ItemsViewRenderer. As for the documentation, CollectionViewRenderer is implementing a UIView now. I agree that it should be updated.

assanesuzy commented 3 years ago

Is there any update on this?