2016-10-14 94 views
1

我是iOS和Xamarin的新手。我創建了CollectionView,其中主框架的全部爲WidthHeight。現在我想要使用150Height和完整Width將收集視圖大小更改爲父母Horizontal Scroll。我嘗試更改代碼,但它不起作用。如何更改iOS中collectionView的大小

的CollectionView全屏:

UICollectionViewDataSource source; 
     UICollectionViewFlowLayout layout; 
     UICollectionView collectionView; 

    protected ViewController(IntPtr handle) : base(handle) 
    { 
     layout = new UICollectionViewFlowLayout 
     { 
      SectionInset = new UIEdgeInsets(20, 5, 5, 5), 
      MinimumInteritemSpacing = 5, 
      MinimumLineSpacing = 5, 
      ItemSize = new SizeF(100, 100), 


     }; 

     collectionView = new UICollectionView(UIScreen.MainScreen.Bounds, layout); 
     collectionView.ContentSize = new CGSize(View.Frame.Size.Width, View.Frame.Size.Height); 
     collectionView.BackgroundColor = UIColor.White; 
     source = new UICollectionViewDataSource(); 

     collectionView.RegisterClassForCell(typeof(TextCell), TextCell.CellId); 
     collectionView.Source = source; 
    } 

輸出:

enter image description here

我預計:

寬度=父寬度,

高度= 150

所以我更改代碼以這種

layout = new UICollectionViewFlowLayout 
      { 
       SectionInset = new UIEdgeInsets(20, 5, 5, 5), 
       MinimumInteritemSpacing = 5, 
       MinimumLineSpacing = 5, 
       ItemSize = new SizeF(100, 100), 


      }; 

      collectionView = new UICollectionView(UIScreen.MainScreen.Bounds, layout); 
      collectionView.ContentSize = new CGSize(View.Frame.Size.Width, 150); 
      collectionView.BackgroundColor = UIColor.White; 
      source = new UICollectionViewDataSource(); 

      collectionView.RegisterClassForCell(typeof(TextCell), TextCell.CellId); 
      collectionView.Source = source; 

而且它在全屏幕中顯示。

任何幫助被讚賞。

回答

1

最後我創造一個View並添加CollectionView爲子視圖並最終這工作,我期待與全Width150Height

工作代碼:

UICollectionView collectionView; 
     UICollectionViewDataSource source; 
     UICollectionViewFlowLayout layout; 



     protected ViewController(IntPtr handle) : base(handle) 
     { 
      layout = new UICollectionViewFlowLayout 
      { 
       SectionInset = new UIEdgeInsets(20, 10, 5, 5), 
       MinimumInteritemSpacing = 5, 
       MinimumLineSpacing = 5, 
       ItemSize = new SizeF(120, 130), 
       ScrollDirection = UICollectionViewScrollDirection.Horizontal 
      }; 

      UIView topcontainer = new UIView(); 

      topcontainer.Frame = new CGRect(0f, 0f, View.Frame.Width, 150f); 

      View.AddSubview(topcontainer); 

      collectionView = new UICollectionView(topcontainer.Bounds, layout); 
      collectionView.ContentSize = new CGSize(topcontainer.Frame.Size.Width, topcontainer.Frame.Size.Height); 
      collectionView.CollectionViewLayout = layout; 
      collectionView.BackgroundColor = UIColor.White; 
      source = new UICollectionViewDataSource(); 

      collectionView.RegisterClassForCell(typeof(TextCell), TextCell.CellId); 
      collectionView.Source = source; 

      topcontainer.AddSubview(collectionView); 
     } 

輸出:

​​