Resizing UITableView with a table header in UIScrollView using auto layout - ios

I have one simple UIView and three tables nested in the UIScrollView. The first two tables have table headers included. The third (orange) table has no header view and it is resized correctly according the data it uses. The picture displays the closed state of the scroll view. UITableView_A and UITableView_B are closed, UITableView_C opened. The data are ready for use in all tables, and also the cells are being created.
Also in xib I have connected the layout constraints to the properties from the subclass of UIScrollView. The constants are specific sizes as UIScrollView demands it for its nested views.
The issue:
The views are being resized correctly but when I update the constants for heights of the tables without the header views (the first two tables, green and pink) I manipulate only with the header views. Which is odd. The auto layout constraints for the green and the pink table are setup correctly in the xib file. I was checking the resized tables in the simulator using introspection tool and I realized the first two resized tables are not tables BUT only the header views.
The height of cells is 44 and headers also 44. The tables are not scrollable.
Closed state:
Opened state
When I update the auto layout constants related to any table I call this line afterward
[self layoutIfNeeded]; //(scroll view)
UPDATE 1:
When I remove the header views from the tables, then the resizing works perfectly for the tables. I am really curious why I manipulate just with the header views instead of the whole table when the constraints is defined on the table view NOT on the table header view.

I replaced the table header views with the section header views and the resizing works smoothly. I'd be glad to know the explanation for the previous problem.

Related

how to increase the height of scroll viewheight dynamically in swift 3?

How can i set the constraints that here two table views are using so that both are dynamical and i need to change the height of scroll view so that both table views and it needs to fit the whole screen and my layout will be as shown below in the image
why you are using tableview in scrollview ? It is not good approach to use scrollable entity into scrollview! You can take one tableview and can create multiple cell for every type of your content like payment view, cart view etc. You can use multiple section also as per requirement. For example, your Table details should be your first section, that can contain multiple rows.
By this way your height will be automatically managed by tableview.

ios 8 nested auto sized tables

I'm pretty new to Auto Layout, so this may be an easy question to answer. I have two UIViewControllers which both contain UITableViews that need to auto-size their cells. I'm trying to use Auto Layout and the new iOS 8 auto-sizing table cells. There is a parent/child relationship between these tables: the parent table view will display the child's table view in its cells.
The child's table view seems to auto-size just fine (the cells at least). The problem is that the parent cell seems unable to determine the size of the child table view.
What I see in a debug session is that the parent cell appears to be truncating the child's view. To be more specific, when I inspect the view hierarchy, I can see that the child table view's cells are auto-sized correctly, but the child table view seems to be set to the size of the parent cell (or some sort of default value) and not to its contents.
I have a repro project that demonstrates the problem. I think it may have something to do with UIScrollView and Auto Layout. I've read other posts on this topic which seem related. If that's the case, I need a little help figuring out how to apply that to my repro project.

How should I approach structuring this view?

I'm having a hard time finding the best way to structure this design.
The top view has a minimum height and becomes sticky when it reaches this height. The bottom view hosts a paging controller with three views within. Each of these views hosts either a collection view or table view with vertical scrolling.
I'm really at a loss on how to approach this. Is the entire view scrollable and I should prevent scrolling on the second view until the top view has reached it's sticky height? Or are each of these views separate uitableviews and the pagingcontroller is just one cell? Should I even be using a pagingcontroller or should I use a scrollview with paging enabled? (the latter was a little rough interaction-wise)
Thank you!
Take a look at the Advanced User Interfaces using Collection View from WWDC this year. This view is very very very similar to the iTunes Connect app interface. The entire session video explains how they created that interface.
I used a similar method to this to create the keyboard in the Emojicate app.
I think what I'd do is actually fake the sticky header. So something like this...
Use only one collection view.
Create a "segmented data source" that contains three data sources. (See the video from WWDC about this)
When the segmented control is changed then update the collection view by changing its layout and (if you want) dataSource.
Make the entire top section a header on the collection view.
When the collection view scrolls past a certain point (when you want to sticky the header) then have a second view that is the compressed header and make it visible at the top of the screen. This is not attached to the collection view at all.
When the segmented control changes you can update the collection view by changing the "selected datasource". The datasource can also contain a UICollectionViewLayout that will update it.
Essentially, the tableview you are talking about is just a collection view where the cell width is equal to the screen width. i.e. fake a table view.
The sticky header isn't sticky at all. Just when it starts to go off screen you can put a fake header there instead.
It will require a duplicate (ish) view and some thinking about how to structure the data but I think this will be easier and less resource hungry than having multiple collection views and page controller and stuff.
If you want me to go through it in more detail let me know but it's a complex subject. Watch the video first.
I would make this part a navigation bar. Should be relatively easy. Just have to customize the back button with a barButtonItem and do a couple of labels in the titleView.
I would make the next part a Table View.
The tableView has 2 sections. The first section doesn't have a section header and the second section doesn't have any cells but just a section header.
First and only cell in this section:
And the rest would be the second section header's view:
This gives you the stickiness that you want because the section header will remain there even if you scroll past it and since the collection has only 2 sections the controls will always remain on top.
I think the collection/table paging part is the hardest part and I don't know clearly how it can be done. But I was thinking it could perhaps be a ContainerView. Each view of the container view would be either a tableview or a collectionview. You would have to add some code to handle the movement of the containerview relative to the second section header (possibly an autolayout constraint that attaches the containerview to the buttom of the first tableview that you implemented above).
I don't think having your tables/collections in a scrollview would be a good implementation. I think I have even read in documentation that developers should stay away from that (but I might be remembering it incorrectly).
I would have:
A "header view" with three subviews:
Fixed height top and bottom views (they stay visible at any size).
A middle view that appears/disappears as the superview grows/shrinks.
A scroll view (table or collection view are subclasses) on that partially covers the header view with a top inset set enough to reveal the underlying header view (the same way pull to refresh views are revealed).
The paging buttons could be set as table/collection view section header views.
Finally track the scroll view's scroll position to keep manually adjusting the header view height.
Another way to see this solution.
Two completely separated parts, a header view and a table view.
A simple header view (blue) that adjusts its subviews as its height changes. More precisely hides its middle subview (light blue) when it shrinks.
A table view that a) partially covers the header view in Interface builder but b) has a top inset as to avoid hiding the header view in the actual device (tableView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);).
The two parts are only "connected" by resizing the header view height as the table view scrolls up/down.

How to dynamically resize table views inside a scroll view

I want to display two table views inside a uiscrollview and make the table views resize dynamically to fit their content. The table views should not be scrollable, hence the embedding in a scroll view. I have managed to resize the scroll view appropriately. The setup is as follows:
UILabel1
TableView1
..some extra space..
UILabel2
TableView2
I suspect that this should be possible with auto layout, but I need help to get started (I'm new to auto layout). Furthermore, I know that the labels could be added to the headers of the table views, but for now this will do. The data is fetched asynch over a network connection, so the table views need to be updated when the data is in.
Optionally, a couple of spinner could occupy the table view space until the data is ready to be displayed.

How can I set the entire header view to UICollectionView?

I want to set the header to collection view, just like UITableViewController's setTableHeader does.
I found the way to set the each section's header on collection view, but I couldn't find how to set the header view of the entire header.
Apple's reference of UICollectionViewLayout says "Supplementary views present data but are different than cells. Unlike cells, supplementary views cannot be selected by the user. Instead, you use supplementary views to implement things like header and footer views for a given section or for the entire collection view. Supplementary views are optional and their use and placement is defined by the layout object.", but I don't know how to implement header view for the entire collection view.
I tried to implement this by making a section header view rectangle only to the first section, but it was different with tableHeader's that.
In table view, if I set the content offset to below header, the content size automatically expands so that the header is hidden although there exist a few elements.
But in collection view, modifying content offset only works fine when there exist many elements enough to make a scroll.
I tried to dynamically expand content size when there exist a few elements, but it seems that it doesn't work fine...
I implemented this using a custom layout and repositioning the header on every scroll event. Start with subclassing UICollectionViewLayout, there are tons of tutorial on the web, this is a good one: http://markpospesel.wordpress.com/2012/10/25/fixing-circlelayout/
Then the trick is to always return YES to shouldInvalidateLayoutForBoundsChange:, and in prepareLayout reposition (ie. change the layoutAttributes' frame) the header based on the collection view xOffset and yOffset.
Use a supplementary view with NSIndexPath 0-0.
layoutAttributesForElementsInRect: will be called on every frame, so if you have a lot of items in your collection view, you will need to cache the result so it does not impact performance. All attributes should be untouched except for that one header.
Source: I spent 1 month working on this particular topic.

Resources