How to vertically center UICollectionView content

uicollectionview center cells horizontally
create a tag flow layout with uicollectionview
vertical uicollectionview swift
uicollectionview center selected cell
uicollectionview horizontal scroll github
uicollectionview scroll horizontal and vertical swift
uicollectionview grid layout swift
uicollectionviewflowlayout

I am trying to dynamically center the content of a UICollectionView, however none of the existing solutions take into account the fact that there might be more than one cell per row.

I have tried this center custom title in UINavigationBar? as well as both solutions here: UICollectionView vertically Centred, to no avail.

I am quite surprised iOS offers no way of doing this by default and even more that it is not possible to get the number of rows currently being displayed (which would've solved my issue as well). Also, I cannot get the size of the content (and not simply the size of the frame) which would also have helped me achieve the desired effect.

I have this:

 --------
|X X X X |
|X X X X |
|        |
|        |
 --------

But I want this:

 --------
|        |
|X X X X |
|X X X X |
|        |
 --------

Any help would be greatly appreciated,

Many thanks.

Now I understand your problem. Depending on the screen size you need another inset to make it look centered. Then you have to calculate it manually, but it should not be that hard. You know the available space, the number of items and the cell size. Just calculate the section inset from top.

How to center align the cells of a UICollectionView?, 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. CollectionView Cells Horizontally Center Alignment Position. step 1: set the collection view Scroll Direction : Horizontal. step 2: set the Constraint to collection view. – height fixed. – vertically centre – set (zero) leading space and trailing space to source view. step 3: Before Using the below code know about the following terms. – frame width

Not sure why you can't get the size of the content — UICollectionView is a UIScrollView subclass, hence has access to the contentSize property. If you only need to center the cells in case they are all visible without scrolling, you can simply do the following:

collectionView.contentInset.top = max((collectionView.frame.height - collectionView.contentSize.height) / 2, 0)

This will also work in cases when the number of cells grows so that not all of them are visible on the screen and you start using vertical scrolling — the top inset would be set to 0.

CollectionView Cells are lining up vertically not horizontally · Issue , Unfortunately, my collectionView cells are not lining horizontally but vertically. Any idea what's going on? Or what I may be missing? Icon Bar Menu Icon Accordion Tabs Vertical Tabs Tab Headers Full Page Tabs Hover Tabs Top Navigation Responsive Topnav Navbar with Icons Search Menu Search Bar Fixed Sidebar Side Navigation Responsive Sidebar Fullscreen Navigation Off-Canvas Menu Hover Sidenav Buttons Sidebar with Icons Horizontal Scroll Menu Vertical Menu Bottom Navigation Responsive Bottom Nav Bottom Border Nav Links Right Aligned Menu Links Centered Menu Link Equal Width Menu Links Fixed Menu Slide Down Bar on Scroll Hide

UICollectionView has a visibleCells property that returns an array. You can use the count property on NSArray to get the number of visible cells.

You should be able to implement - collectionView:layout:insetForSectionAtIndex: in your delegate implementation to set the top inset to something that will result in the centering you're shooting for.

You can also subclass UICollectionViewFlowLayout and implement your own layout logic if necessary.

UICollectionViewFlowLayout subclass to vertically align cells at the , Hello @alloy,. Your code didn't work for me because the call to self.itemSize.​height did not give the correct value. I found the following code  Use CSS vertical-align Property The vertical-align property is used to center vertically inline elements. The values for vertical-align align the element relative to its parent element: Line-relative values vertically align the element relative to the entire line.

If you have a tabBar do this

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

    let navBarHeight       = self.navigationController?.navigationBar.frame.height
    let collectionViewHeight = (self.collectionView?.frame.height)! - navBarHeight!
    let itemsHeight        = self.collectionView?.contentSize.height

    let topInset = ( collectionViewHeight - itemsHeight! ) / 4

    return UIEdgeInsetsMake(topInset ,0, 0 , 0)
}

If not ignore the tabBar substraction and leave the rest...

AlignedCollectionViewFlowLayout on CocoaPods.org, A collection view layout that gives you control over the horizontal and vertical alignment of the cells. You can use it to align the cells like words in a left- or right-​  Click the “Page Layout” tab. Click the “Page Setup” button in the lower-right corner of the “Page Setup” section of the “Page Layout” tab. On the “Page Setup” dialog box, click the “Layout” tab. In the “Page” section, select “Center” from the “Vertical alignment” drop-down list.

Add this in viewDidLoad

let viewHeight: CGFloat = view.center.y
let contentHeight: CGFloat = collectionView.contentSize.height
let marginHeight: CGFloat = (viewHeight - contentHeight) / 2.0
self.collectionView.contentInset = UIEdgeInsets(top: marginHeight, left: 0, bottom:  0, right: 0)

Centre UICollectionView Cells Horizontally, Collection View, available in the UICollectionView class, are a new concept in iOS 6. It introduce ContentView – This view contains the content that the cell presents. It is rendered in the height fixed. – vertically centre. You set the line-height as we did above (It’ll need to be greater than the height of the image) and then set vertical-align: middle on the image. CSS Table Method. Above I mentioned that vertical-align applies to table cells which leads us to this method.

Creating your own Centered CollectionView Flow Layout, Today we'll take a look at how to tackle a commonly designed CollectionViewLayout where items are centered and how to customise it! The UICollectionView offers greater customisation, allowing you to set a custom UICollectionFlowLayout. Unfortunately, the grid layout scrolls along one axis only; either horizontally or vertically. We hoped we could embedded the collection inside a UIScrollView, allowing us to scroll in both direction, but after some attempts we decided to

UICollectionView Horizontal and Vertical Scroll in Swift, A vertically scrolling collection view with exactly enough items to fill the screen. data source calculating each cell's position within the collection and attributes for the cells in your data source and updating the content size. You can vertically align your content/modules within a column using custom spacing (padding and margin). For example, you can use Divi’s spacing options to give a column equal top and bottom padding to center the module(s) vertically within the column. Or, you can add only top padding to a column to make the content bottom aligned.

‍ 🤟 How to vertically center the contents of a UICollectionView, Now I understand your problem. Depending on the size of the screen, you need another insert to make it look centered. Then you have to calculate it manually,  Vertical center in a div with transform This uses transform to center any content in a div vertically. This method uses an HTML empty element as a container, in this case p .

Comments
  • I don't understand your problem. And what do you mean by how to "get" the number of rows? You tell the collection view the number of items, their size, etc...
  • @Darko: In order to center a group of cells I need to be able to know how many rows the collection view is displaying (which is different to the number of items as multiple items can be displayed in a row, depending on the screen size). Once I have the number of cells I can set the top inset to (collectionview frame height - total height occupied by the cells) / 2. I have added a visual representation to my question.
  • In regard to your visual representation - if you want version 2, why do you make the collection view so big? Just make it smaller and center it in the screen with auto layout. Apart from this (as jorf already answered) you could set the section insets. But in my opinion the insets are only useful if you want to change it during runtime. If the layout is fixed just make the collection view smaller.
  • @Darko because depending on the screen size I get a very different layout. On iphone 6s for 12 items, I get two rows of 6 cells. On an iphone 4s I get 6 rows of 2 items. Not sure I can account for that with auto layouts.
  • Ah, ok, now I understand your problem. Depending on the screen size you need another inset to make it look centered. Then you have to calculate it manually, but it should not be that hard. You know the available space, the number of items and the cell size. Just calculate the section inset from top.
  • can you write codes here ? I understand what you mean But I don't know which codes are suitable for this
  • See my second comment to Darko as to why this is not a viable solution. This assumes I have the same amount of cells for every row whatever the size of the screen. Although all cells are visible, they will not be displayed on a constant set of rows.
  • the problem is still exist for me can you help me please ? here is my question link stackoverflow.com/questions/49727912/…