How can I center rows in UICollectionView?

前端 未结 9 1689
囚心锁ツ
囚心锁ツ 2021-02-02 06:24

I have a UICollectionView with random cells. Is there any method that allows me to center rows?

This is how it looks by default:

[ x x x x         


        
9条回答
  •  忘了有多久
    2021-02-02 07:25

    Just link this flow layout. You can align center, left, right also.

     //
        //  CellAllignmentFlowLayout.swift
        //  UICollectionView
        //
        //  Created by rajeshkumar Lingavel on 8/11/15.
        //  Copyright © 2015 rajeshkumar Lingavel. All rights reserved.
        //
    
    import UIKit
    enum   SZAlignment:Int {
        case Center,
            left,
            Right
    }
    class CellAllignmentFlowLayout: UICollectionViewFlowLayout {
        var alignment:SZAlignment!
        var padding:CGFloat!
        override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    //        NSArray *allAttributesInRect = [super
    //            layoutAttributesForElementsInRect:rect];
    
            let allAttributesInRect:NSArray = super.layoutAttributesForElementsInRect(rect)!
            var changedAttributes:NSArray = NSArray()
            switch(alignment.rawValue){
                case 0:
                   changedAttributes  =   alignCenter(allAttributesInRect)
                case 1:
                    changedAttributes =  alignLeft(allAttributesInRect)
                case 2:
                    changedAttributes =  alignRight(allAttributesInRect)
                default:
                    assertionFailure("No Direction")
            }
    
            return changedAttributes as? [UICollectionViewLayoutAttributes]
        }
    
    
       private func alignCenter(allAttributesInRect:NSArray) -> NSArray{
    
    
            let numberOfSection:Int = (self.collectionView?.numberOfSections())!
    
            let redefiendArray = NSMutableArray()
    
            for  i in 0 ..< numberOfSection {
    
                    let thisSectionObjects = sectionObjects(allAttributesInRect, section: i)
                    let totalLines = numberOfLines(thisSectionObjects, section: i)
                    let lastrowObjects = lastRow(thisSectionObjects, numberOfRows: totalLines, section: i)
                    let lastRowObjectsRow =  setMiddleTheLastRow(lastrowObjects)
                    let start = (thisSectionObjects.count - lastrowObjects.count)
    
                    for j in start.. NSArray{
    
    
            return allAttributesInRect;
    
        }
       private func alignRight(allAttributesInRect:NSArray) -> NSArray{
            return allAttributesInRect;
    
        }
    
       private func getTotalLenthOftheSection(section:Int,allAttributesInRect:NSArray) -> CGFloat{
    
            var totalLength:CGFloat = 0.0
            totalLength = totalLength + (CGFloat (((self.collectionView?.numberOfItemsInSection(section))! - 1)) * padding)
            for  attributes in allAttributesInRect {
    
                if(attributes.indexPath.section == section){
                    totalLength = totalLength + attributes.frame.width
                }
            }
    
            return totalLength
        }
    
       private func numberOfLines(allAttributesInRect:NSArray,section:Int)-> Int{
            var totalLines:Int = 0
            for  attributes in allAttributesInRect {
                if(attributes.indexPath.section == section){
                    if (attributes.frame.origin.x == self.sectionInset.left){
                        totalLines = totalLines + 1
                    }
                }
            }
            return totalLines
        }
       private func sectionObjects(allAttributesInRect:NSArray,section:Int) -> NSMutableArray{
            let objects:NSMutableArray = NSMutableArray()
            for  attributes in allAttributesInRect {
                if(attributes.indexPath.section == section){
                    objects.addObject(attributes)
                }
            }
            return objects
        }
    
       private func lastRow(allAttributesInRect:NSArray,numberOfRows:Int,section:Int) -> NSMutableArray{
            var totalLines:Int = 0
            let lastRowArrays:NSMutableArray = NSMutableArray()
            for  attributes in allAttributesInRect {
                if(attributes.indexPath.section == section){
                    if (attributes.frame.origin.x == self.sectionInset.left){
                        totalLines = totalLines + 1
                        if(totalLines == numberOfRows){
                            lastRowArrays.addObject(attributes)
                        }
                    }
                    else{
                        if(totalLines == numberOfRows){
                            lastRowArrays.addObject(attributes)
                        }
                    }
                }
            }
            return lastRowArrays
        }
       private func setMiddleTheLastRow(lastRowAttrs:NSMutableArray)->NSMutableArray{
            let redefinedValues = NSMutableArray()
            let totalLengthOftheView = self.collectionView?.frame.width
            var totalLenthOftheCells:CGFloat = 0.0
            totalLenthOftheCells = totalLenthOftheCells + (CGFloat (lastRowAttrs.count) - 1) * padding
    
            for attrs in lastRowAttrs{
                totalLenthOftheCells = totalLenthOftheCells + attrs.frame.width
            }
    
            var initalValue = (totalLengthOftheView!/2) - (totalLenthOftheCells/2)
    
            for  i in 0..

提交回复
热议问题