How to center a cell in UICollectionView? - ios

I have a UICollectionView which consists of 12 cells from Jan to Dec as shown in the screenshot below:
How can I make the current month to always to be at the center of the collection view? For example, Dec shall be shown on the center for this month.
One of the solutions is to scroll automatically to the next page and center the 12th cell but how can I achieve that?

You can use the cellForItem function to define how the cells are going to appear and the UICollectionView function reloadData() to update all the cells always you think it's necessary.
If December should be the cell in the middle, make sure that 5 or 6 cells come before December, you should be able to do this with simple conditions inside cellForItem.
For example, as you have 12 cells, you can get the current month as and integer (12 for December), and you will know that the first month will be 12 - 6 = 6 (June), so you can do this calculation for each indexPath.
To simplify your job:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = ...
let month = GetCurrentMonthAsInteger()
var cellMonth = month - 6 + indexPath.row //or section if you have 12 sections
if(cellMonth < 1) {cellMonth = 12 + cellMonth}
else if(cellMonth > 12) {cellMonth = cellMonth - 12}
cell.setInformationForMonth(cellMonth)
...
return cell
}

You can try making min spacing to zero for cells and lines

Related

Custom Calendar By 3 UICollectionViewCell

I want to create a calender with use of only 3 collectionViewCell.
I have achieved this by 12 UICollectionViewCell for 1 year:
.
but now I want to make it work by just 3 collectionViewCell.
I want to implement below logic. but not getting idea how to do.
For example, current scenario is:
indexPath.item 0 - cell for previous mointh(JAN),
indexPath.item 1 - cell for current month(FEB),
indexPath.item 2 - cell for next month(MAR).
Now,
when user scrolls to next month(MAR), at that time I want to remove indexPath.item = 0(JAN), and shift remaining cells on left.
after that add next month cell(APR) at indexPath.item = 2
so it will be like this after one scroll:
indexPath.item 0 - cell for previous mointh(FEB),
indexPath.item 1 - cell for current month(MAR),
indexPath.item 2 - cell for next month(APR).

How to center align CollectionView Cell?

Hi have a collectionview.
Each cell's width is screenwidth/3. Means 3 cells in a row.
Now. If have 10 or 11 cells then there will be 4 rows. and there are 1 or 2 cells in last row.
The problem is ,I want to align these cell to center without changing its size . now cells are left align.
Note: I want to achieve this with in a section(Because collection-view already have multiple sections).
Image attached what i want to achieve .
You can solve this using multiple sections. Number of section should be
numberOfSection = totalItemCount/3 > Int(totalItemCount/3) ? Int(totalItemCount/3) + 1 : Int(totalItemCount/3)
dont use ceiling. Then number of items in section should be
numberOfItemInSection = totalItemCount-section*3 < 3 ? totalItemCount-section*3 : 3
Then implement the UICollectionViewDelegateFlowLayout delegate. Where in collectionView(_ collectionView:, layout:, insetForSectionAt:) set,
let minimumInterItemSpacing: CGFloat = 8.0
// minimumInterItemSpacing should be same in the delegate function
let itemCountInSection = collectionView.numberOfItems(inSection: section)
let edgeSpace = (collectionView.bounds.width - ((itemCountInSection*self.itemWidth) + (minimumInterItemSpacing*(itemCountInSection-1)))) / 2
return UIEdgeInsets(top: self.topSpace, left: edgeSpace, bottom: self.bottomSpace, right: edgeSpace)
set other delegate methods as it suits you.

Not all cells are shown in a CollectionViewController Swift

I'm currently developing a periodic table and to show the table I'm using a collection view. The periodic table has 118 elements and I'm trying to show them all in the same page but when I run the app on the simulator, only 77 cells are shown.
Here's the code I used in my Swift file: source code
I was wondering how can I show all the cells on the same screen. I searched online but I couldn't find anything.
steps:
1. first count the number of items or element in your periodic table, i.e divide the whole frame in to grids, to see how many small square boxes are there..
now create a modal for your elements, say for first item Hydrogen, it goes in [Datamodel] array 0 index, and for next 16 array element its empty object as there is no element.. in this way create your modal
now create your collectionview cells and populate the data with your modal, where the data are blank object show black image, and for those where there is an element, show respectively
now for the size of each element, there is a UICollectionViewDelegateFlowLayout method
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
let width = self.collectionview.frame.size.width/18 as there are 18 columns
return CGSize(width: width, height: width)
}
this way all your cells will be visible in horizontal view.. but then again all cell might not be visible in landscape mode, like elements with 58, 59
so for height of each element you need to do
let height = height/9 as there are 9 rows
but this way, you. might not get square cells

Different line spacing for each item at UICollectionView - swift 3

I have a collection view written programmatically with swift 3. It has 1 section and 6 vertical items in it. The default minimum line spacing for each item is 10 I assume, but I want to set different line spacing for each item. For example, the space between item 0 and 1 become 50 but space between item 1 and 2 become 0. How can I do that? I searched a lot but couldn't find solution. Thanks in advance.
It is directly not possible by setting some property of your collection view but you can do one trick I think,
set your minimum line spacing and minimum interitem spacing to 0 like,
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
so your collection view cell will not have any space at any side!
now keep your cell's background color as clear color!
and add UIView (your content) in your cell with size(less than cell size) that shows spacing between two cell. So, you can add different size of view in every cell and it will display like different space between every cell!
Or
If you don't want to resize your inner view of cell then you can return different size for your cell from delegate method sizeForItemAtIndexPath for different items!
For example,
I am writing objective c code for example,
your sizeforitem,
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.item == 1 || indexPath.item == 6 || indexPath.item == 7 || indexPath.item == 9 || indexPath.item == 15) {
return CGSizeMake(screenWidth/3 -20, screenWidth/2);
}
return CGSizeMake(screenWidth/3, screenWidth/3);
}
and result is
An idea could be to consider the lines spacing as "other kind of cell", like blank cell, you see? And in the method heightForRowAtIndexPath, when the indexPath correspond to those blank cell, you return their custom height, otherwise you return the row height of your "real" cells.
(This imply to return those blank cell in the cellForRow, and add their number to your current numberOfRowInSection count too)

How to adjust UICollectionView contentSize.height based on number of rows in swift

I'd really appreciate if someone can light up some ideas here. I've been trying to fix this for weeks now (not kidding).
I have a "to do list" using a collectionView where I hide the rows that were completed and move them to the end of the list. I then unhide the items if needed with a button. The collectionView looks exactly as a tableView with one item(cell) per row.
When the items are hidden the collectionView has a lot of empty scrolling space at the bottom instead of automatically deleting the space used by the hidden rows since they technically are still there.
I'm trying to cut that empty space so the collectionView height would be equal to the amount of cells/rows left visible.
override func viewDidAppear(animated: Bool) {
if isCellHidden { //checking if cells are hidden
var heightOfSection0 = 95 + (42 * self.collectionView.numberOfItemsInSection(0))
println(self.collectionView.contentSize.heigh) //returns 2350
self.collectionView.contentSize.height = CGFloat(heightOfSection0)
println(heightOfSection0) //returns 1019
println(self.collectionView.contentSize.heigh) //returns 1019.0 which is correct but as soon as I scroll down it resets to it's original size (2350) and let's me scroll through that empty space...
}}
If I try to read the collectionView height immediately after setting this, it displays the correct value but as soon as I try to scroll down it resets back to it's original height. I also tried disabling the auto layout and it doesn't make a difference
You should not manage contentSize directly - return appropriate number of items to be displayed from collectionView(_:numberOfItemsInSection:) instead (i.e. do not count your "hidden" cells).
You can use sizeForItemAtIndexPath: to change the size of collection view cell.
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var numberOfCellInRow : Int = 3
var padding : Int = 5
var collectionCellWidth : CGFloat = (self.view.frame.size.width/CGFloat(numberOfCellInRow)) - CGFloat(padding)
return CGSize(width: collectionCellWidth , height: collectionCellWidth)
}
Changing UI stuff in viewDidAppear() in theory is a good place to do so. However, at this point, you can't be sure that auto layout has operated on the subview that you're manipulating.
You can check when it has and when it's safe to manipulate the frame, by subclassing it and overriding layoutSubviews()
I had a similar issue here: https://stackoverflow.com/a/30446125/4396258

Resources