How to center align the cells of a UICollectionView?

前端 未结 18 2463
梦如初夏
梦如初夏 2020-11-28 01:33

I am currently using UICollectionView for the user interface grid, and it works fine. However, I\'d like to be enable horizontal scrolling. The grid supports 8

相关标签:
18条回答
  • 2020-11-28 02:08

    Based on user3676011 answer, I can suggest more detailed one with small correction. This solution works great on Swift 2.0.

    enum CollectionViewContentPosition {
        case Left
        case Center
    }
    
    var collectionViewContentPosition: CollectionViewContentPosition = .Left
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
        insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    
        if collectionViewContentPosition == .Left {
            return UIEdgeInsetsZero
        }
    
        // Center collectionView content
    
        let itemsCount: CGFloat = CGFloat(collectionView.numberOfItemsInSection(section))
        let collectionViewWidth: CGFloat = collectionView.bounds.width
    
        let itemWidth: CGFloat = 40.0
        let itemsMargin: CGFloat = 10.0
    
        let edgeInsets = (collectionViewWidth - (itemsCount * itemWidth) - ((itemsCount-1) * itemsMargin)) / 2
    
        return UIEdgeInsetsMake(0, edgeInsets, 0, 0)
    }
    

    There was an issue in

    (CGFloat(elements.count) * 10))

    where should be additional -1 mentioned.

    0 讨论(0)
  • 2020-11-28 02:09

    In Swift, the following will distribute cells evenly by applying the correct amount of padding on the sides of each cell. Of course, you need to know/set your cell width first.

    func collectionView(collectionView: UICollectionView,
        layout collectionViewLayout: UICollectionViewLayout,
        insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    
            var cellWidth : CGFloat = 110;
    
            var numberOfCells = floor(self.view.frame.size.width / cellWidth);
            var edgeInsets = (self.view.frame.size.width - (numberOfCells * cellWidth)) / (numberOfCells + 1);
    
            return UIEdgeInsetsMake(0, edgeInsets, 60.0, edgeInsets);
    }
    
    0 讨论(0)
  • 2020-11-28 02:11

    I think you can achieve the single line look by implementing something like this:

    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
        return UIEdgeInsetsMake(0, 100, 0, 0);
    }
    

    You will have to play around with that number to figure out how to force the content into a single line. The first 0, is the top edge argument, you could adjust that one too, if you want to center the content vertically in the screen.

    0 讨论(0)
  • 2020-11-28 02:11

    Here is how you can do it and it works fine

    func refreshCollectionView(_ count: Int) {
        let collectionViewHeight = collectionView.bounds.height
        let collectionViewWidth = collectionView.bounds.width
        let numberOfItemsThatCanInCollectionView = Int(collectionViewWidth / collectionViewHeight)
        if numberOfItemsThatCanInCollectionView > count {
            let totalCellWidth = collectionViewHeight * CGFloat(count)
            let totalSpacingWidth: CGFloat = CGFloat(count) * (CGFloat(count) - 1)
            // leftInset, rightInset are the global variables which I am passing to the below function
            leftInset = (collectionViewWidth - CGFloat(totalCellWidth + totalSpacingWidth)) / 2;
            rightInset = -leftInset
        } else {
            leftInset = 0.0
            rightInset = -collectionViewHeight
        }
        collectionView.reloadData()
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0, leftInset, 0, rightInset)
    }
    
    0 讨论(0)
  • 2020-11-28 02:12

    Not sure if this is new in Xcode 5 or not, but you can now open the Size Inspector through Interface Builder and set an inset there. That'll prevent you from having to write custom code to do this for you and should drastically increase the speed at which you find the proper offsets.

    0 讨论(0)
  • 2020-11-28 02:15

    You can use https://github.com/keighl/KTCenterFlowLayout like this:

    KTCenterFlowLayout *layout = [[KTCenterFlowLayout alloc] init];
    
    [self.collectionView setCollectionViewLayout:layout];
    
    0 讨论(0)
提交回复
热议问题