Align Left UICollection View Cell. RRS feed

  • Question

  • User348461 posted

    Hi I am creating collection view with several size of labels.

    these labels have same hight but there width is change dynamically.

    This is the code of my collection view layout.

    **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)
                var frame = currentLayoutAttributes.Frame;
                frame.X = previousLayoutEndPoint + maximumSpacing;
                currentLayoutAttributes.Frame = frame;
            return attributes;

    My question is when i am having one item in my collection view it display on the center, but i need to display it on the left side.

    if i change EstimatedItemSize = new CGSize(50f, 35f) in to ItemSize = new CGSize(50f, 35f) it display correctly but then the width is not change dynamically.

    I am new to the xamarin.ios is any one can help really appreciate.

    Monday, October 16, 2017 2:30 PM


  • User348461 posted

    I was able to do this, i set the first cell like this.

    var firstCellFrame = attributes[0].Frame; firstCellFrame.X = 0; attributes[0].Frame = firstCellFrame;

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, October 26, 2017 11:06 AM