问题描述
有人知道如何在 Xamarin.iOS 中对 UICollectionView
进行子类化吗?
Does anybody know how to subclass a UICollectionView
in Xamarin.iOS?
我试过了
public class MyCustomUICollectionView : UICollectionView
{
[Export ("initWithFrame:")]
public InfinitiveScrollingUICollectionView (CGRect frame) : base(frame)
{
// initialization
}
}
但我明白
UIKit.UICollectionView.UICollection(Foundation.NSCoder) 的最佳重载方法匹配有一些无效参数参数 #1 无法将
CoreGraphics.CGRect表达式转换为类型
Foundation.NSCoder`.
我也尝试使用 public InfinitiveScrollingUICollectionView ()
但我得到了
I also tried to use public InfinitiveScrollingUICollectionView ()
but I get
类型 UIKit.UICollectionView
不包含接受0"参数的构造函数
我想覆盖 LayoutSubviews
.还是应该将 UICollectionViewController
用于这样的目的?
I want to override LayoutSubviews
. Or should the UICollectionViewController
be used for such a purpose?
推荐答案
UICollectionView
没有接受单个 CGRect
的构造函数,所以你也必须传递一个布局:
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)
{
}
}
这篇关于子类化 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!