问题
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 convert
CoreGraphics.CGRectexpression to type
Foundation.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