testing
testing

Reputation: 20279

Subclassing UICollectionView

Does anybody know how to subclass a UICollectionView in Xamarin.iOS?

I tried this

public class MyCustomUICollectionView : UICollectionView
{

    [Export ("initWithFrame:")]
    public InfinitiveScrollingUICollectionView (CGRect frame) : base(frame)
    {
        // initialization
    }
}

but I get

The best overloaded method match for UIKit.UICollectionView.UICollection(Foundation.NSCoder) has some invalid arguments Argument #1 cannot convertCoreGraphics.CGRectexpression to typeFoundation.NSCoder`.

I also tried to use public InfinitiveScrollingUICollectionView () but I get

The type UIKit.UICollectionView does not contain a constructor that takes '0' arguments

I want to override LayoutSubviews. Or should the UICollectionViewController be used for such a purpose?

Upvotes: 1

Views: 800

Answers (1)

asp_net
asp_net

Reputation: 3597

UICollectionView has no constructor that accepts a single CGRect, so you have to pass a layout, too:

public class MyCustomUICollectionView : UICollectionView
{
    public MyCustomUICollectionView(CGRect frame, UICollectionViewLayout layout) 
        : base(frame, layout)
    {
    }
}

If you want to, you can also create the layout internally, so you don't have to pass it from the outside:

public class MyCustomUICollectionView : UICollectionView
{
    private static readonly UICollectionViewLayout _layout;

    static MyCustomUICollectionView()
    {
        // Just an example
        _layout = new UICollectionViewFlowLayout();
    }

    public MyCustomUICollectionView(CGRect frame) : base(frame, _layout)
    {
    }
}

Upvotes: 3

Related Questions