2017-10-17 36 views
0

Je crée une vue de collection avec plusieurs tailles d'étiquettes. Ces étiquettes ont toutes la même hauteur mais leurs largeurs sont changées dynamiquement.Aligné à gauche Cellule UICollectionView Lorsqu'il a un élément dans Xamarin.ios

C'est le code de mon point de vue collection mise en page:

EstimatedItemSize = new CGSize(50f, 35f); 

MinimumInteritemSpacing = 10f; 

MinimumLineSpacing = 10f; 

public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(CGRect rect) 
{ 
var attributes = base.LayoutAttributesForElementsInRect(rect); 

     for (var i = 1; i < attributes.Length; ++i) 
     { 
      var currentLayoutAttributes = attributes[i]; 
      var previousLayoutAttributes = attributes[i - 1]; 
      var maximumSpacing = MinimumInteritemSpacing; 
      var previousLayoutEndPoint = previousLayoutAttributes.Frame.Right; 
      if (previousLayoutEndPoint + maximumSpacing + currentLayoutAttributes.Frame.Size.Width >= CollectionViewContentSize.Width) 
      { 
       continue; 
      } 
      var frame = currentLayoutAttributes.Frame; 
      frame.X = previousLayoutEndPoint + maximumSpacing; 
      currentLayoutAttributes.Frame = frame; 
     } 
     return attributes; 
    } 

Ma question est: Quand j'ai un élément dans ma collection voir est affichée au centre, et LayoutAttributesForElementsInRect méthode ne sera pas appelé. Mais j'ai besoin de l'afficher sur le côté gauche.

Si je change EstimatedItemSize = new CGSize(50f, 35f) en ItemSize = new CGSize(50f, 35f) il s'affiche correctement, mais la largeur n'est pas modifiée dynamiquement.

+0

Je poster une réponse. Ou vous pouvez utiliser une autre bibliothèque tierce comme ceci: https://github.com/mokagio/UICollectionViewLeftAlignedLayout. –

Répondre

0

Vous pouvez ajouter des codes pour changer la position de la première cellule, lorsque vous utilisez le EstimatedItemSize, comme ceci:

 public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(CoreGraphics.CGRect rect) 
     { 

      var attributes = base.LayoutAttributesForElementsInRect(rect); 


      //Add these lines to change the first cell's position of the collection view. 
      var firstCellFrame = attributes[0].Frame; 
      firstCellFrame.X = 0; 
      attributes[0].Frame = firstCellFrame; 


      for (var i = 1; i < attributes.Length; ++i) 
      { 
       var currentLayoutAttributes = attributes[i]; 
       var previousLayoutAttributes = attributes[i - 1]; 
       var maximumSpacing = MinimumInteritemSpacing; 
       var previousLayoutEndPoint = previousLayoutAttributes.Frame.Right; 
       if (previousLayoutEndPoint + maximumSpacing + currentLayoutAttributes.Frame.Size.Width >= CollectionViewContentSize.Width) 
       { 
        continue; 
       } 
       var frame = currentLayoutAttributes.Frame; 
       frame.X = previousLayoutEndPoint + maximumSpacing; 
       currentLayoutAttributes.Frame = frame; 
      } 
      return attributes; 
     } 

Il fonctionne très bien comme ceci:

enter image description here