Scrolling UICollectionView horizontally with a UITextField inside the cell? - ios

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

Related

Programmatically Scroll at specific item or offset, with UIKit Compositional Layout collectionView?

I have an iOS 14 collectionView, configured with compositional layout.
You can scroll vertically through the collectionView, and you can scroll horizontally within the sections.
I would like to know if there is a way to scroll programmatically, within a section?
Something like collectionView.setContent(offset: 100, inSection: 2)
And is there a way to get the contentOffset of a particular section?
I can't find any function to do that.
Sincerely,
Jery
Its a hack but for such sections the parent is a UIScrollView which you can get a reference to by referencing a cell in the section and then referencing its superview.
Like this:
let cell = collectionView.cellForItem(at: indexPath)!
let scrollView = cell.superview as! UIScrollView
scrollView.setContentOffset(<#CGPoint#>, animated: true)

Change scrollDirection on a subclass of UICollectionViewFlowLayout

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
}

Ignore vertical scrolling inside UITableViewCell

One of the cells in a UITableView contains a scroll view. I want to be able to scroll the content in the cell horizontally, but NOT vertically.
How can I achieve this?
Additionally, the scroll view is a subview of UIWebView, so I cannot control its content size.
I have tried setting the content offset directly, but this prevents the entire table from being scrolled. I want the table to scroll vertically, but not the content in the cell.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
DispatchQueue.main.async {
scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: 0)
}
}
You will have to ensure the content is smaller than the vertical bounds of the UIScrollView to prevent vertical scrolling, and in addition you'll need to set scrollView.alwaysBounceVertical = false.

Sending heightForRow:atIndexPath height value after calculating it in-cell

I have a UITableView that has few cells with different heights. Each row's is dynamically adjusted according to the content it contains.
I have a custom UITableViewCell with a UICollectionView in it that should be resized according to how many cells it has (all the cells should be visible, without inside scrolling).
I also have a UITableViewCell with another UITableView that should be resized according to how many cells it has (all the cells should be visible, without inside scrolling).
The problem is that I don't have the contentSize of the collectionView and the tableView while heightForRow:atIndexPath gets called so I can't set the values to something right.
I've tried to set it to UITableViewAutomaticDimension and to set the cell.contentView.frame.size.height to the contentSize when it got set (added an observer on "contentSize") but then the cells were on top of each other (the collectionView was ontop of the tableView instead of above it).
The tableView code is a regular tableView code.
The collectionView code is:
self.collectionView.collectionViewLayout = UICollectionViewLeftAlignedLayout()
let collectionViewFlowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
collectionViewFlowLayout.estimatedItemSize = CGSize(width: 34, height: 50)
What is the best way to adjust the size of the tableView and the collectionView?
Thank you!
You can just ask UITableView to adjust its cell height again with this piece of code:
self.tableView.beginUpdates()
self.tableView.endUpdates()

Disable vertical scrolling in UICollectionView programmatically

I have a UICollectionView in a UITableViewCell. The height of the table view cell is set to the height of the collection view so the collection view only scrolls horizontally.
Sometimes when scrolling in the table view the collection view will capture the vertical scrolls and bounce scroll vertically. I've set the height to 0 in -collectionViewContentSize in my custom layout.
How do I completely disable vertical scrolling in a collection view?
In your storyboard - click your UICollectionView and open Utilities. Under the Attributes Inspector, center button, look to 'Bounces'. Uncheck "Bounces Vertically".
To completely disable vertical scrolling in a UICollectionView programatically, add the following to your viewDidLoad() method
self.collectionView.isScrollEnabled = false
Example:
class YourCollectionView: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.isScrollEnabled = false
}
// Then your methods for creating cells and layout, etc
}

Resources