How to align UICollectionViewCells from left to right? - ios

I should design UICollectionView like below image.
It was almost done. but my UICollectionView is displaying like below image.
I thought it would be solved if I set semanticContentAttribute of UICollectionView to forceLeftToRight. but the result was not changed.
How can I design that like first image?

forceLeftToRight simply forces a left to right layout even when the locale is a right to left locale, so this doesn't help since UICollectionViewFlowLayout centers the cells within its view.
The complicated way to do what you want is to subclass UICollectionViewFlowLayout and override layoutElementsForItem(at:) to place the last cell on the left.
The simpler way is to add an additional invisble/empty cell in the case where there is an odd number of cells.

Related

How to set Table view cell to occupy entire screen?

I am working on a project to get custom news feeds from Bing news. I created a custom table view cell and populated various UI items in it. But I am having a hard time to make it occupy the entire screen, it starts from extreme left and ends on the right, leaving large gap from the right margin as show in the image. Can anyone help me out, how to correctly fix it with a detailed explanation of how constraints work in Table views and Tableview cells.
Here' what it looks like in the simulator
Remove previous constraints and add constraint to your UITableView like as shown in below image.
So you UITableView will have 0 margin from edges.
Create a UITableViewController. Just drag drop a TableViewCell into it. Add identifier for that cell and use it. By default it will occupy the whole screen.
In your case I guess you might have added constraints to UITableView. Here what you should do is pin your UITableView to Top,Bottom,Right & Left or make UITableView's width equal to SuperView.

UICollectionView that functions like a tableview

The designer came back with a layout that displays information differently.
Is it possible to make my UICollectionView function like a UITableView? Where each cell takes up one row and there is only one column?
Everything I found on the net seems to be about the opposite, making a UITableView be a UICollectionView
Actually, its very simple. Just make the width of the UICollectionViewCell equal to the width of the view.

UICollectionView two cells in a single row

I am trying to layout a vertical scrolling collection view cells to layout like this
When I tried to size the cells like that it made the bottom right one go onto the next line instead of group them together.
Take a look at the UICollectionViewLayout class. There you should find methods you need to achieve your desired look.
https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UICollectionViewLayout_class/index.html

iOS: CollectionView stick header while scrolling

I am implementing CollectionView with custom layout. It have vertical scrolling.
I want to make that some cells will be sticked to the top while scrolling like section header in tableView.
The idea is to create day collectionView with time. And while user will be scroll thought time the current hour will be sticked to the top(if it will be not visible and hidden by top). It will be only if the current hour hidden by top, not by bottom. And the other items should be scrollable like usual.
I think that I should not use supplementaryViews here
Is it possible and how can I do it?
You need to override shouldInvalidateLayoutForBoundsChange so it returns YES. This means layoutAttributesForSupplementaryViewOfKind and layoutAttributesForElementsInRect are called when the UICollectionView is scrolled, and in there you can then calculate proper frames for your views, including the sticky views. If you have a complicated UICollectionView, this may lead to bad performance, but as far as I know it's the easiest way to do this for now. Your other bet is looking at invalidation contexts, but they're quite badly documented. If you still want to experiment with those, you could take a look at Using Invalidation Contexts for UICollectionViewLayout
Take a look at UICollectionView's supplementaryView.
Apple's Collection View Programming Guide

UICollectionView scroll single column

I have a problem, it is not so much about a bug, but rather how to approach the issue.
I wanna create a UICollectionViewLayout where I can scroll a single column. Normally when
we scroll a scroll view, all the subviews scroll up/down/left/right. But here I wanna have a set of columns, and basically as user scrolls vertically, I want to only scroll that column and leave the others where they were. More over if the user scrolls horizontally I want the normal behavior, that is, all columns scroll left or right.
Any idea how to approach this problem ?
It's a little late, but, better late than ever!
You are right in that you need to rock your own UICollectionViewLayout. I recommend you start with having it work normally before you start working on your particular behaviour.
Basically what you want to do is have all cells but one column be vertically sticky.
To achieve that, you need reposition all your cells (but the ones that actually move) based on the Collection View's xOffset and yOffset.
Checkout my answer on this post for more information: https://stackoverflow.com/a/20987008/322377
For this you can use a UITableView with custom UITableViewCell containing UIScrollView and you can set scrollview to scroll in desired direction.

Resources