Matching widths for Autolayout views in separate hierarchies - ios

I am trying to build a spreadsheet like app which has a scroll view and "floating" headers on the top and left which do not scroll with the rest of the content. I have this implemented in a ScrollView using layoutSubViews to override the frames of the header UIViews, with all the scrolling in a single UIView:
ScrollView
Top Header UIVew
Left Header UIVew
Content UIView
Content
This is all working great, except where the items in the header need to line up with the main content (i.e., "W:cell1==topHeader1", "H:cell1==leftHeader1", etc.). Autolayout won't allow constraints since the items are not in the same hierarchy. How do I keep the widths/heights the same?

I think it's better implementing this scenario using UICollectionView, if the default UICollectionViewFlowLayout won't satisfy your requirement subclass it or even create a whole new custom layout.
Such scenario is definitely for CollectionView, it will add reusability for the visible cells. So that if you have hundreds of rows/columns, you don't use extra memory.

Related

Complex cross layout architecture and separation of concerns

I want to implement a cross layout just like Wallapop app does on its main feed.
As you can see, it's composed of two groups of cells (Featured items, Items near you). The first group is scrolled horizontally, and the second group is scrolled vertically.
The first UIKit component that came to my mind to make that kind of layout is UICollectionView, having one section for each scrolling direction. Unfortunately UICollectionView current implementation is very limited, forcing the scroll in one direction only, no matter how many sections you declare.
So I wanted to give it a shot with vanilla UIKit components and that's what I got;
The problem with my solution is that the vertical UICollectionViewController (highlighted in green) is scrolling on its own and not pushing the horizontal UICollectionViewControllers upward.
I've also thought about using a single UICollectionViewController for the vertical cells, and setting an UIStackView with horizontal UICollectionViewControllers as needed for the horizontal cells, but it's a messy solution and doesn't scale very well, I even couldn't set a title for the vertical cells section If I opt this way.
Ideally, I want each group of scrollable cells to be it's own UIViewController in order to have a clear separation of concerns and modularity.
Is there a better way to implement a layout like the one I want with vanilla UIKit components?
The easiest and the most stable solution is to use the following view hierarchy:
Use one UICollectionView(1) instance
Horizontally scrollable sections can be implemented as Screen width UICollectionViewCell containing horizontally scrollable UICollectionView
Vertically scrollable sections should be just a regular section of UICollectionView(1)
Pros
UIKit only
Nothing extraordinary is needed - just a UICollectionViewFlowLayout everywhere
Cells are the same for Horizontally scrollable cells and Vertically scrollable cells
Good scalability and separation of concerns. Independent behaviors of Horizontal and Vertical sections each of which can have multiple data sources.
Cons
Horizontally scrollable sections should have fixed height. Otherwise scrolling behavior will be harder to maintain.
Arrow on the image means Uses!
You could use following way:
A collection view with two cell in it. First cell is for featured items and the second cell is for items near you. The first cell will cover full width and height required by your design.
For scalability use a Container View which embed a CollectionViewController which will responsible for showing featured item. This embedded CollectionViewController will scroll horizontally.
Items near you are straight forward direct cell of the initial collection view controller.
So your implementation of featured horizontal collection view remain in the embedded CollectionViewController and the items for cell remains in the primary CollectionViewController which also contain a loose connection (embedding into a container view) to the featured item.
It also does not suffer from the problem like scrolling vertically the items for sale does not scroll up the featured items.
Here is a screenshot which depicts the idea

Multipage UICollectionView with View

I am trying to create a collection view layout similar to the one used by Twitter and Product Hunt. I want to have a horizontal collection view to tab between data while also maintaining vertical scrolling in the tab I am in. I do not want to just change the datasource that is loaded into the UICollectionView. I have tried several different layouts but none of them have been successful.
Right now, I have a UIViewController with a UICollectionView & UIImageView object inside of it. The UIImageView's size is (3/8) of the UIViewController's frame and begins at (0,0). The UICollectionView's frame is equal to the UIViewController's frame but the cv is offset to be below UIImageView & its size is equal to the size of the UIViewController's view. The (1) cell inside the UICollectionView has a UICollectionView inside of it -- this can be configured with the data you want (multiple cells). This layout works fine for a single datasource.
However, when you want to have multiple datasources accessed by scrolling horizontally the layout breaks down. You have to change the flow layout of the outermost UICollectionView to horizontal. When you do this, you get a layout error because the size of the collection view is larger than than the view and you cannot scroll vertically.
If you reference the image I provided, I think you can infer what the desired layout should behave like. You should be able to access the other datasource by horizontal scrolling but also be able to scroll the current collection view full screen and not have it fixed to the bottom of the UIImageView. I tried adding logic to the scrollview delegate methods but that didn't work either. Thanks]2

One section of the view controller is still and one section horizontally scrollable

I would like to construct a view controller where one section of the view controller would be still and one section scrollable.
Both sections have headers where as well, one is still and one is moving along with the content in the section.
I do not want the cells in the section to be scrolled separately. All cells should move at the same time along with the header.
I have added an image to make my point little more clearer.
Use UICollection View for both view and disable scrolling for one view and enable scrolling for another view
you can probably add to your UIViewController view a UITableView on the left with fixed size (for example 150px) and vertical scroll disabled and a UICollectionView with horizontal flow and ,if needed a, with custom UICollectionViewLayout (but i think that you just need the classic UICollectionViewFlowLayout) for the right part that fits the remaining space.
Here you can find the component's documentation:
https://developer.apple.com/reference/uikit/uicollectionview
https://developer.apple.com/reference/uikit/uitableview

Two UIViews & one UICollectionView inside UIScrollView (or better approach)

I need to have iOS app with screen like this:
screen
The idea is when user start to scroll down the first UIView to move up until the second UIView reach the top where it will stick and only UICollectionView will continue to move up.
Currently I'm using this structure
UIScrollView (main scroll)
UIView (some banners)
UIView (UISegmentedControl)
UICollectionView (grid with items, scroll is disabled, main scroll is used)
I manage to do it, but I needed to set UICollectionView height constraint manually in the code (calculated based on all items in grid) in order to appear in UIScrollView (everything else is handled by AutoLayout in Storyboard). The problem with this is that UICollectionView think all cells are visible and load them, so the whole recycling & reusing thing does not work. It's even worst because I use willDisplayCell method on UICollectionView to load more data when last cell is displayed, but now it load all pages at once.
My question (actually they are 2)
How can I fix the issue above?
What is the right way to achieve this functionality? Maybe my whole approach is conceptually wrong?
Collection view is a scroll view itself. So maybe you could have same functionality only with Collection view with sections or even custom layout?

horizontal scrolling for each section in a table view

I want to implement a UICollectionView with multiple sections which each section scrolls horizontally and independently like the attached image
I suggest you to use UITableView with customized UITableViewCells with UIScrollView as subview on its contentViews.
There are many samples, for example here
You can use multiple UICollectionViews in table cells. The tableView will scrolls vertically and The collection views can be configured to scroll horizontally by setting particular set of properties which will restrict them to scroll vertically, and they will only scroll horizontally. One constraint to consider is it is much more difficult to do animations that need to move from one table cell to another and you can't use a neat single change of collection view layout to animate all the items in your table view. But if these constraints aren't a problem then this is a relatively easy solution. I also tried it once. It worked for me. Hope that it works for you as well.

Resources