Subclassing UICollectionView

扶醉桌前 提交于 2021-02-08 07:21:04

问题


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?


回答1:


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)
    {
    }
}


来源:https://stackoverflow.com/questions/29302301/subclassing-uicollectionview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!