Ios 如何在xamarin.tvos中将标题正确添加到我的收藏视图
我已经在我的集合视图中添加了一个标题,但它没有按我希望的方式显示。我试图让标题显示在我的视图单元格的顶部和左侧,但是,我仍然坚持你在所附图片中看到的内容。我真的很感谢你帮我解决这个问题。谢谢Ios 如何在xamarin.tvos中将标题正确添加到我的收藏视图,ios,xamarin.ios,tvos,uicollectionreusableview,Ios,Xamarin.ios,Tvos,Uicollectionreusableview,我已经在我的集合视图中添加了一个标题,但它没有按我希望的方式显示。我试图让标题显示在我的视图单元格的顶部和左侧,但是,我仍然坚持你在所附图片中看到的内容。我真的很感谢你帮我解决这个问题。谢谢 /*Header*/ public class Header:UICollectionReusableView { public const string identifier = "header"; public static UIL
/*Header*/
public class Header:UICollectionReusableView
{
public const string identifier = "header";
public static UILabel text
{
get { return text; }
set
{
text.Text = "Header";
text.TextAlignment = UITextAlignment.Left;
text.TextColor = UIColor.White;
text.TranslatesAutoresizingMaskIntoConstraints = false;
}
}
public Header(CGRect frame): base(frame)
{
AddSubview(text);
text.TopAnchor.ConstraintLessThanOrEqualTo(SafeAreaLayoutGuide.TopAnchor).Active = true;
text.HeightAnchor.ConstraintLessThanOrEqualTo(SafeAreaLayoutGuide.HeightAnchor).Active = true;
text.LeadingAnchor.ConstraintLessThanOrEqualTo(SafeAreaLayoutGuide.LeadingAnchor).Active = true;
text.TrailingAnchor.ConstraintLessThanOrEqualTo(SafeAreaLayoutGuide.TrailingAnchor).Active = true;
}
public Header(IntPtr handle) : base(handle) { }
[Export("initWithCoder:")]
public Header(NSCoder coder) : base(coder) { }
public override void LayoutSubviews()
{
base.LayoutSubviews();
text.Bounds = Bounds;
}
}
/*ViewController*/
public class SectionViewController : UICollectionViewController
{
[Export("initWithCollectionViewLayout:")]
public SectionViewController(UICollectionViewLayout layout) : base(layout)
{
}
public SectionViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// initialize the method that launches all the collection views registered
CollectionView.RegisterClassForSupplementaryView(typeof(Header), UICollectionElementKindSection.Header, Header.identifier);
CollectionView.RegisterClassForCell(typeof(UICollectionViewCell), "cells");
}
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
CollectionView.Frame = new CGRect(0, 0, View.Frame.Width, View.Frame.Height/2);
//CollectionView.TranslatesAutoresizingMaskIntoConstraints = false;
CollectionView.BackgroundColor = UIColor.Blue;
}
[Export("numberOfSectionsInCollectionView:")]
public override nint NumberOfSections(UICollectionView collectionView)
{
return 1;
}
[Export("collectionView:numberOfItemsInSection:")]
public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return 20;
}
[Export("collectionView:cellForItemAtIndexPath:")]
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var cell = (UICollectionViewCell)collectionView.DequeueReusableCell("cells", indexPath);
cell.BackgroundColor = UIColor.SystemGreenColor;
cell.Layer.CornerRadius = 10;
return cell;
}
[Export("collectionView:viewForSupplementaryElementOfKind:atIndexPath:")]
public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
var header = (Header)collectionView.DequeueReusableSupplementaryView(elementKind, Header.identifier, indexPath);
header.BackgroundColor = UIColor.SystemRedColor;
return header;
}
}
/*AppleDelegate*/
public class AppDelegate : UIApplicationDelegate
{
// class-level declarations
public override UIWindow Window
{
get;
set;
}
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
// Override point for customization after application launch.
// If not required for your application you can safely delete this method
// create a new window instance based on the screen size
Window = new UIWindow(UIScreen.MainScreen.Bounds);
var layout = new UICollectionViewFlowLayout();
var controller = new SectionViewController(layout);
layout.ScrollDirection = UICollectionViewScrollDirection.Horizontal;
layout.ItemSize = new CGSize((controller.CollectionView.Frame.Width - 50) / 1.1, (controller.CollectionView.Frame.Width - 10) / 6);
layout.MinimumLineSpacing = 50.0f;
layout.MinimumInteritemSpacing = 10.0f;
layout.SectionInset = new UIEdgeInsets(0, -200, 100, 0);
layout.SectionHeadersPinToVisibleBounds = true;
layout.HeaderReferenceSize = new CGSize(controller.CollectionView.Frame.Size.Width / 3, controller.CollectionView.Frame.Size.Height / 9);
var navController = new UINavigationController(controller);
Window.RootViewController = navController;
// make the window visible
Window.MakeKeyAndVisible();
return true;
}
我认为奇怪的红色方块可能是由于标题中的错误布局造成的,我在文本标签中没有看到您。我在这里写了一个例子,效果很好:
public class Header : UICollectionReusableView
{
public const string identifier = "header";
public UILabel text { get; set; }
[Export("initWithFrame:")]
public Header(CGRect frame) : base(frame)
{
text = new UILabel();
text.Frame = new CGRect(0, 0, 100, 50);
AddSubview(text);
}
}
public class SectionViewController : UICollectionViewController
{
[Export("initWithCollectionViewLayout:")]
public SectionViewController(UICollectionViewLayout layout) : base(layout)
{
}
public SectionViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// initialize the method that launches all the collection views registered
CollectionView.RegisterClassForSupplementaryView(typeof(Header), UICollectionElementKindSection.Header, Header.identifier);
CollectionView.RegisterClassForCell(typeof(UICollectionViewCell), "cells");
}
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
//CollectionView.TranslatesAutoresizingMaskIntoConstraints = false;
UICollectionViewFlowLayout layout = this.Layout as UICollectionViewFlowLayout;
CollectionView.Frame = new CGRect(0, 0, View.Frame.Width, View.Frame.Height / 2);
layout.ItemSize = new CGSize(100, 100);
layout.MinimumLineSpacing = 50.0f;
layout.MinimumInteritemSpacing = 10.0f;
layout.SectionInset = new UIEdgeInsets(0, -200, 100, 0);
layout.SectionHeadersPinToVisibleBounds = true;
layout.HeaderReferenceSize = new CGSize(300, 100);
CollectionView.BackgroundColor = UIColor.Blue;
}
[Export("numberOfSectionsInCollectionView:")]
public override nint NumberOfSections(UICollectionView collectionView)
{
return 1;
}
[Export("collectionView:numberOfItemsInSection:")]
public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return 20;
}
[Export("collectionView:cellForItemAtIndexPath:")]
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var cell = (UICollectionViewCell)collectionView.DequeueReusableCell("cells", indexPath);
cell.BackgroundColor = UIColor.SystemGreenColor;
cell.Layer.CornerRadius = 10;
return cell;
}
[Export("collectionView:viewForSupplementaryElementOfKind:atIndexPath:")]
public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
var header = (Header)collectionView.DequeueReusableSupplementaryView(elementKind, Header.identifier, indexPath);
header.BackgroundColor = UIColor.SystemRedColor;
header.text.Text = "qweqweq";
return header;
}
}
还有一些例子,请随时问我任何问题