I have a collectionView that scrolls vertically. Inside the cells of that collectionView is another collectionView that I have a custom layout for. The layout is currently a subclass of UICollectionViewFlowLayout and I want the scroll direction to be horizontal. I have tried a variety of ways to set the scrollDirection to horizontal but none of them are working. How do I set scroll direction on a subclass of FlowLayout?
You can easily change scroll direction by this code:
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .horizontal
}
Related
in my app, I have a collection view in center of my screen, like this:
but in my app I need that cell at indexpath.row 1 always be center of my screen. like this
is there a solutions (in swift) for this situation?
thanks guys
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
collectionView!.collectionViewLayout = layout
UICollectionViewFlowLayout has 2 properties for insets:
minimumInteritemSpacing
minimumLineSpacing
I need to have a view controller that has a scroll in view.
The problem is that I need that both scrolls (of scrollView and collectionView) work together.
But when I add ScrollView in all frame, nothing works...
That's an image that shows what I want:
And when I scroll viewController, all content scroll together..
Someone know a good way to implement this in swift 4?
Thanks in advance.
I've seen this done many ways. The most popular of which is to have these views embedded in a UITableView.
You can set the first cell height to a proportional amount to the superview, while keeping another cell that will contain your CollectionView. You would be able to take advantage of the UITableView's scrollView as well as it's memory efficiency and delegate.
A better way to do this is to use the scroll direction in UICollectionViewDelagateFlowLayout. Just add the UICollectionViewDelegateFlowLayout protocol or extends your ViewController to it. Then use .scrollDirection in the section of your UICollectionView.
And sample declaration like this
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
...
layout.scrollDirection = .vertical //or .horizontal depends on your liking
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
...//
return cv
}()
then add to your view view.addSubview(collectionView)
More info in the apple doc or jump to definition
So I have UICollectionViewCells that contain a textField inside but when I try to scroll collectionView with my finger on the textField, the collectionView doesn't scroll. I've tried setting delaysContentTouches to false but to no avail.
Here's the collectionView that I'm trying to scroll but won't let me due to the textField:
UITextField inside a UICollectionViewCell
EDIT:
I can scroll horizontally outside of the textField and cell, but I'm trying to scroll the collectionView by touching and scrolling the textField (I have the logic to dismiss the keyboard if scrolled in case anyone finds this a bad idea).
Try this
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
self.collectionView.collectionViewLayout = layout
I'm having a dilemma maybe someone can help. I followed this tutorial that showed me how to dynamically adjust the height of a collectionview to match the height of the text.
https://possiblemobile.com/2016/02/sizing-uicollectionviewcell-fit-multiline-uilabel/
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.estimatedItemSize = CGSizeMake(1, 1) }
cell.label.preferredMaxLayoutWidth = 50
But the issue I'm having is now the collection view wraps around the text instead of conforming to collectionView.bounds.width. So when i have text that is smaller than the collectionview width, the cell wraps around the text. I want the cell to match the height of the text but have a set width set to collectionview bounds. How could I achieve this?
I set up a collection view and got all working with fixed height and width for the cells.
Now I would like to have my Cells self sized, so I found that I can add
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.estimatedItemSize = CGSize(width: 200, height: 50)
menuCollectionView.collectionViewLayout = layout
}
With above code my cell resizes by the width (as wanted), but my collection view is not scrollable horizontally anymore, but instead lays out the cells vertically.
Is estimatedItemSize only thought to work for vertical scrolling? What is wrong with my approach?
The default scroll direction of a UICollectionViewFlowLayout is vertical, so you need to set the scrollDirection for the new layout, too:
layout.scrollDirection = UICollectionViewScrollDirection.Horizontal