I am trying to implement something like this:
I have a UITableView with 1. "static" section, the total number of sections in the tableView can vary. I always want the first section (white area) to be visible to the user, the remaining sections should scroll underneath the first section. I have tried to implement this with two UITableViews, but since the (white area) can vary in size, I can't set a definite frame. I am using Storyboards with autolayout. At the moment the best solution I have come up with is the two UITableViews, but I need to find a way that I can resize the two tableViews according to the content of the white area and according to each other. The white area, one of the tableViews is containing a section with two rows, the first row is containing text that can vary in length and therefore needs dynamic resizing.
Any Idea how I can tackle this? Can I change the NSLayoutConstraints dynamically somehow?
If i understand correct, your first UIView rectangle if fixed, bottom table is scrollable.
To implement this, you should create typical UIViewController, add UIView and UITableView (programmatically or through an outlets). You should manage each view (tableView and UIView) separately.
Related
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
I have UITableView with height of ≤500. Tableview data comes from database. Below UITableView, there is one label and two radio button. The problem is if data in the UITableView is less than its height, then it shows blank space between UITableView and those two radio button. I want to place those label and radio button exactly below tableview. How should I do this ?
This is how my tableview looks
If the table view's height does indeed vary depending on shown content, you could use UITableView's tableFooterView.
For a bit more context see this post for example.
Alternatively you could do this with basic auto-layout by tying your label and radio buttons (that you place as siblings of the table) vertically to the bottom of the table view.
I could also imagine that you may need a section footer. So you may want to have a look at that too.
If you want it to be scrollable, add it as the last row in your table view. You may also go with table view's footer.
If you want it to be sticky at the bottom in case of more rows, go with auto layouts. You may create outlet of table view's height constraint and adjust that accordingly depending on the number of rows. However, this approach is not appreciated.
A question for the UICollectionView experts.
Imagine a collection view that looks like a table view (full width cells), and when you tap on one of them, new cells are inserted underneath that cell that are square cells, say half the width of the collection view, in 2 columns. Tapping the header again would appear to collapse the section.
Keeping in mind that I’m trying to use UICollectionViewFlowLayout instead of a custom UICollectionView.
Would you:
A) implement the tableview style cells as collection view supplementary views (headers), with a gesture recogniser that inserts the square cells?; or
B) implement the tableview style cells as one cell type, and the square cells as another cell type?; or
C) something else?
Personally I would go with option A. In the situation you're describing the full width item is really a section header that's function is to show/hide it's sections's cells.
From a data structure standpoint this is much easier to maintain with this approach. It can be handled with an array of arrays.
If they were all cells it would most likely be one large array, and would be more difficult to make the distinction between a full width cell and the half width cells.
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
I am trying to configure my CollectionView to have one section be differently sized than the others. I have two sections. Lower part should show a number of cells simultaneously and the upper section should only show one cell at a time but be scrollable to reveal more cells one by one.
I tried to play with the .frame property of the CollectionView but obviously it is not the right approach as it changes the appearance of the whole view.
I also tried to retrieve the FlowLayout object and see if I can get it from there. Did not find a way.
Neither the section Insets are the answer so far ...
It is simple to use two uicollectionviews for upper and lower section, instead of using only one.