提问人:Unnamed 提问时间:3/5/2018 最后编辑:Unnamed 更新时间:3/5/2018 访问量:494
Xamarin iOS。我可以在stackView控件中使用集合视图(作为stackview的项目)吗?
Xamarin iOS. Can I use collection view inside of stackView control (as item of stackview)?
问:
我有一个简单的应用程序,其中包含自定义。控制器放置在 Mainstoryboard 中(我使用本机 xamarin)。我的应用程序可以工作,但我想在我的集合视图上方添加一个通用的“标题”控件(带有与/描述集合一起使用的按钮和标签)。
我本来以为,当我已经创建的集合视图位于 StackView 的底部,而我新创建的“标头”控件位于顶部时,就可以完成它。但正如我所看到的,我不能将我的 as 项目添加为 .
我认为这可以通过 来完成,但看起来这不是一个好主意,特别是如果我有多个用于我的收藏视图的电位控制按钮。
也许有更好的解决方案?
有人可以建议我吗?UICollectionViewController
UICollectionViewCell
UIStackView
UICollectionViewController
Stack view
TabBarController
答:
你可以创建一个新的,在里面放一个。然后初始化你的 in ,在 StackView 中添加它的视图:UIViewController
UIStackView
UICollectionViewController
Storyboard
UIView headerView = new UIView();
...//Add some items in this view
//Add a height constraint
headerView.AddConstraint(NSLayoutConstraint.Create(headerView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, 200));
MyStack.AddArrangedSubview(headerView);
MyCollectionView collection = Storyboard.InstantiateViewController("MyCollectionView") as MyCollectionView;
MyStack.AddArrangedSubview(collection.View);
但是如果你只想添加一个标题视图,我建议你使用 的 .UICollectionView
Header
首先,使用下面的事件在 CollectionView
的 :Source
public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
if (elementKind == "UICollectionElementKindSectionHeader" && indexPath.Section == 0)
{
UICollectionReusableView header = collectionView.DequeueReusableSupplementaryView(new NSString("UICollectionElementKindSectionHeader"), "MyHeader", indexPath);
//Add some items
//header.AddSubview();
return header;
}
return null;
}
不要忘记注册 Header 类:CollectionView.RegisterClassForSupplementaryView(typeof(UICollectionReusableView), new NSString("UICollectionElementKindSectionHeader"), "MyHeader");
最后设置标题的高度以显示它:
// This event exists in the UICollectionViewDelegateFlowLayout
public override CGSize GetReferenceSizeForHeader(UICollectionView collectionView, UICollectionViewLayout layout, nint section)
{
if (section == 0)
{
return new CGSize(UIScreen.MainScreen.Bounds.Size.Width, 300);
}
return CGSize.Empty;
}
您可以通过在故事板中创建 Storyboard 并拖放 And 作为其中的子视图来添加您的子视图。并将视图控制器指定为集合视图的数据源属性
对于 Common 标头控件,您可能希望通过创建 的子类来使用或通过编程方法创建自定义。UICollectionView
UIStackView
UIViewController
UIStackView
UICollectionView
UIView
XIB
UIView
因此,您的控制器层次结构将是
->UIViewController
->UIStackView
->UIView
->UICollectionView
但是,非常适合您拥有复杂视图层次结构的情况,您不必自己处理多个自动布局约束,但它有一些常见问题,您可能需要查看这篇文章。UIStackView
如果不是这种情况,那么您应该使用更简单的方法,只需手动添加约束来布局视图。
评论