将 UICollectionViewFlowLayout 的 ItemSize 设置为设备的宽度
Set ItemSize of UICollectionViewFlowLayout to width of device
我正在尝试使用 Xamarin 创建一个 UICollectionViewFlowLayout,其中我的单元格将是设备的宽度并垂直向下流动。类似于 Facebook 新闻选项卡 iOS 应用程序。
目前获取布局中的Cells,这里通过代码:
public override async void ViewWillAppear (bool animated)
{
try{
base.ViewWillAppear (animated);
var layout = new UICollectionViewFlowLayout ();
layout.MinimumInteritemSpacing = 30;
var collectionView = new NewsListView (UIScreen.MainScreen.ApplicationFrame, layout);
collectionView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
collectionView.BackgroundColor = UIColor.LightGray;
collectionView.RegisterClassForCell (typeof(NewsListViewCell), cellId);
collectionView.ReloadData ();
CollectionView = collectionView;
Logger.Log(""+CollectionView.Bounds.Width);
}catch(Exception ex){
Logger.Log (ex);
ShowUserMessage ("Error",ex.Message);
}
}
我想用它来设置项目的大小:
layout.ItemSize = 新 CGSize (CollectionView.Bounds.Width-20, 200.0f);
但是当我创建 UICollectionViewFlowLayout 时,CollectionView 还不存在,
有没有其他方法可以将 UICollectionViewFlowLayout 的大小设置为设备的宽度?
我找到了解决问题的办法。我将布局创建为全局变量,然后将其添加到 ViewDidLoad () 中的视图,然后在 ViewWillAppear(..) 中我可以设置 layout.ItemSize
所以我的代码如下:
UICollectionViewFlowLayout layout;
public override async void ViewDidLoad ()
{
try{
base.ViewDidLoad ();
Title = "News";
layout = new UICollectionViewFlowLayout ();
layout.MinimumInteritemSpacing = 30;
var collectionView = new NewsListView (UIScreen.MainScreen.ApplicationFrame, layout);
collectionView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
collectionView.BackgroundColor = UIColor.LightGray;
collectionView.RegisterClassForCell (typeof(NewsListViewCell), cellId);
collectionView.ReloadData ();
CollectionView = collectionView;
}catch(Exception ex){
Logger.Log (ex);
ShowUserMessage ("Error",ex.Message);
}
}
public override async void ViewWillAppear (bool animated)
{
try{
base.ViewWillAppear (animated);
Logger.Log(""+CollectionView.Bounds.Width);
layout.ItemSize = new CGSize (CollectionView.Bounds.Width-20, 200.0f);
}catch(Exception ex){
Logger.Log (ex);
ShowUserMessage ("Error",ex.Message);
}
}
这样布局的项目宽度总是基于设备宽度而不是硬编码
我正在尝试使用 Xamarin 创建一个 UICollectionViewFlowLayout,其中我的单元格将是设备的宽度并垂直向下流动。类似于 Facebook 新闻选项卡 iOS 应用程序。
目前获取布局中的Cells,这里通过代码:
public override async void ViewWillAppear (bool animated)
{
try{
base.ViewWillAppear (animated);
var layout = new UICollectionViewFlowLayout ();
layout.MinimumInteritemSpacing = 30;
var collectionView = new NewsListView (UIScreen.MainScreen.ApplicationFrame, layout);
collectionView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
collectionView.BackgroundColor = UIColor.LightGray;
collectionView.RegisterClassForCell (typeof(NewsListViewCell), cellId);
collectionView.ReloadData ();
CollectionView = collectionView;
Logger.Log(""+CollectionView.Bounds.Width);
}catch(Exception ex){
Logger.Log (ex);
ShowUserMessage ("Error",ex.Message);
}
}
我想用它来设置项目的大小: layout.ItemSize = 新 CGSize (CollectionView.Bounds.Width-20, 200.0f);
但是当我创建 UICollectionViewFlowLayout 时,CollectionView 还不存在,
有没有其他方法可以将 UICollectionViewFlowLayout 的大小设置为设备的宽度?
我找到了解决问题的办法。我将布局创建为全局变量,然后将其添加到 ViewDidLoad () 中的视图,然后在 ViewWillAppear(..) 中我可以设置 layout.ItemSize
所以我的代码如下:
UICollectionViewFlowLayout layout;
public override async void ViewDidLoad ()
{
try{
base.ViewDidLoad ();
Title = "News";
layout = new UICollectionViewFlowLayout ();
layout.MinimumInteritemSpacing = 30;
var collectionView = new NewsListView (UIScreen.MainScreen.ApplicationFrame, layout);
collectionView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
collectionView.BackgroundColor = UIColor.LightGray;
collectionView.RegisterClassForCell (typeof(NewsListViewCell), cellId);
collectionView.ReloadData ();
CollectionView = collectionView;
}catch(Exception ex){
Logger.Log (ex);
ShowUserMessage ("Error",ex.Message);
}
}
public override async void ViewWillAppear (bool animated)
{
try{
base.ViewWillAppear (animated);
Logger.Log(""+CollectionView.Bounds.Width);
layout.ItemSize = new CGSize (CollectionView.Bounds.Width-20, 200.0f);
}catch(Exception ex){
Logger.Log (ex);
ShowUserMessage ("Error",ex.Message);
}
}
这样布局的项目宽度总是基于设备宽度而不是硬编码