Horizontal and Vertical Scrolling Usign UICollectionView and ScrollView - ios

I have an app in which There is a Card View.
I have put the card in UICollectionViewCell.
My Flow layout is Horizontal.
Cards was scrolling Horizontaly.
Now the problem rises when I have to support iPhone4s.
The Vertical Length of Card is Very long to fit in Screen.So I have to Scroll the Content in UICollectionView Vertically also.
So I Decided to put my UICollectionView inside UIScrollView.
In my viewDidLoad I am setting the content size of UIScrollView.
But it is not scrolling.
What I am missing.??

you can have tableview and then add collection view in tableview cell and thus you can have horizontal as well as vertical scrolling without any interruption.
UITableView->UITableViewCell->UICollectionView in tableview cell->load cells in collectionview

Related

Issues with dynamic height for UITableViewCell with UIStackView while scrolling up

I have a table view with cells with dynamic heights. Inside each cell, I have an image view and a group of 4 labels in a vertical stack view. When I first load the tableview, the cells look fine and the height is calculated correctly. This is also true for the cells that load after scrolling downwards. However, as soon as I scroll up, the height of the cell becomes huge (bigger than the entire screen).
Im not sure why this happens only when I scroll up.
tableview after initial loading
tableview after scrolling upwards
I set the spacing parameter for UIStackView to a value not 0 and it worked!

UICollectionView - vertical scrollling, horizontal paging with custom layout

I'm trying to implement an iOS collection view which basically should work similar to a table view, but the cells' widths being integer multiples of the collection view's width (eg. 4 times the width). the collection view should scroll vertically (like a regular tableview does), but it should page horizontally across the content.
I do have the custom layout working except for the paging. Currently, the collectionView will just scroll horizontally. I'm unsure how to implement the paging correctly.
If I just set pagingEnabled on the collectionView, it has no effect. I suspect that the scrollview needs to be told what the width of a page is..?
Or do I have to do anything weird because there is only one cell/column per row, instead of multiple cells?
See the image; the grey bars are the cells, the blue rect is the collection view frame.
thanks!
I believe if the collection view item widths are less than or equal to the width of the collection view, then the horizontal paging should work via the pagingEnabled property. Otherwise, you need to create the paging effect yourself using the UIScrollView delegate methods and animating to offsets yourself after a certain threshold

In a UICollectionView, how can I get my cells to autolayout BEFORE they appear on screen?

I have a UICollectionView with self sizing cells. When I scroll through the collection I can see the cells resizing themselves at the bottom of the screen.
Is there a way I can get the cells to resize themselves before the appear?
I am using flow layout and my cells contain just 3 labels. I am also using estimatedItemSize.

Vertical UITableView within Horizontal UITableView

I have a horizontal UITableView with pagination enabled, and vertical UITableView within horizontal tableView. Its working fine in less than iOS 7 and not properly working in iOS 7.x.
The problem is, user can not scroll vertically, because somehow horizontal (Outer UITableView) scrolling has higher priority than vertical (inner UITableView) scrolling. How I can disable Outer UTableView scrolling when user scrolls vertical and vice versa
I set the directionalLockEnabled property as YES, but not resolved.
I had this same functionality in an app, but instead of a horizontal UITableView i had a UICollectionView the tableview was vertical and the collection view was horizontal. The functionality is nearly the same. You just have to set the property of the UICollectionView's layout object to be horizontal. In the storyboard, in the sub options of a UICollectionView is a golden cube which is the UICollectionViewFlowLayout object. You can select that and it contains a scrollDirection property to which you can set to either horizontal or vertical.
At any time i was able to pan-gesture vertically or horizontally and there was no problem, i was able to scroll what i wanted to scroll.
UICollectionView Reference

UICollectionView scrolling in both directions

I made a UICollectionView with a vertical scroll.
The width of the cell is more than than the screen width, so I created a customFlowLayout based on UICollectionViewFlow layout returning the right calculated content size.
However, this doesn't work. When the width of the cell is less than the screen width it works. Does it mean that we can't have width more than than screen width in vertical scroll?
It is the same for horizontal scroll, but then the height of the CollectionView is limited to screen height.
Is there any way to make it work?
As others have already said, the UICollectionView can only scroll one direction using a flow layout. However you can accomplish this very easily without creating a custom layout or using a third party library.
When you lay your view out in story board, you can put your UICollectionView embedded in a UIScrollView. Have the scrollview set up to scroll horizontally and the UICollectionView to scroll Vertically. Then set the UICollectionView.delaysContentTouchesto true so touches will pass through to the UIScrollView and not think you are trying to scroll the collectionview.
When you set up the UICollectionView, set it's size and the size of the cells to be what you actually want them to be (Wider than the actual screen) and lay them out accordingly.
Now in the containing UIViewController put this code in the view lifecycle
- (void)viewDidLayoutSubviews {
self.myScrollView.contentSize = self.myCollectionView.frame.size;
}
That's literally all you have to do to accomplish what you are describing. Now your scrollview should allow you to scroll horizontally to view your entire cell and your collectionView should scroll vertically through your cells.
Happy programming.
I'm not sure I've understood your problem.
But if you have made a custom layout, make sure you have implemented :
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
and that your layout attributes frame and size are set with correct values for your "large cell" index path.
Also make sure you have implemented :
- (CGSize) collectionViewContentSize;
This method returns the contentSize of the collection View. If contentSize.width > youAppFrame.width you should have horizontal scrolling. Same for height and vertical scrolling.
Also make sure your collectionView allows scrolling and that your layout is prepared correctly using :
- (void)prepareLayout
By the way, for your layout have you overloaded UICollectionViewLayout or UICollectionViewFlowLayout ?
Before you can do that you MUST use a different type of layout. The flow layout represents its items as a list and it spans these items in cells based on the available width.
If you want to have both horizontal and vertical scrolling you need to somehow specify the number of columns for your grid. the FlowLayout doesn't have that. A simple sollution is to make a subclass of UICollectionViewLayout and override collectionViewContentSize to make it retun a width = to the added sum of the cells widths of one row (this is where knowing how many collumns you want is necessary), plus any additional spacing between them. This will work fine if your cells have the same size per column, similar to a grid.
You should embed a UITableView into a UIScrollView.
ScrollView and TableView will have the same height but different widths.
This way UITableView will scroll vertical and UIScrollView will scroll horizontal.
Xcode 11+, Swift 5.
I solved my issue, I prepared video and code

Resources