IOS Swift: Scrolling child views doesnt scroll the parent view - ios

My iOS app view has a cover Image at top (100% width, 30% height) and a Table for the rest of the screen. To acheive this kind of layout, I am using "Container Views" in my view. So I have Two Container Views:
1st Container is normal view controller with UIImage.
2nd Container is Table View Controller. This can have many entries.
When I run this and try to scroll, only the second container scrolls in place. 1st container remains at the top.
How can I make the full parent view to scroll instead of just second view.
(Are container views right bet for this kind of requirement and layout?)

you may create a new cell called "HeaderImageCell", and make the first cell to be "headerImageCell"
"Container Views" is not needed at this point.

Related

How to get contents of container view to scroll in sync with parent tableview in Swift?

I have a table view controller which contains a view and below that, 2 container views. Each container view containing individual tableviews with some data (see diagram).
A segment control is used to switch between which of the container views are visible.
The content within the container views extend beyond the height of the main screen.
Currently the parent tableview controller is scrolling independently to the table views within the container views which is expected.
How can I sync the container views and parent tableview to scroll together?
Current hierarchy is shown in the diagram below:
Current view controller hierarchy
Usually having this kind of UX/UI is not a good idea. I personally hate this UX.
But to answer your question, you could observe for the contentOffset property of both child tableViews using KVO and update the parent tableView's contentOffset accordingly by doing the appropriate calculations.
Btw, you could use a regular UIViewController and UIScrollView as a containerView instead of the UITableViewController
The ideal approach for me is having a SINGLE tableView, and loading all the content dynamically. F.e. you could have the segment control (and all static data) in the tableViewHeaderView, and depending on which tab is selected you could update the content of the tableView accordingly by switching the array of the first tab's datasource to the second tab's, and vice-versa.
Even if the UI of the cells differs for both tabs, you could dequeue two different cells depending on the selected tab.

UITableViewCells are displayed outside of the table view container

I have a view controller which contains a table view controller embedded in a container view:
What you see inside the view controller on the left is a container view that embeds the table view controller through a storyboard segue. And like you see in the image, there is also a constraint that specifies a top space between the container view and the above segmented control.
When I launch the application everything is normal and I can see a separation space between the segmented control and the table view cells. But as I scroll down, as you see in the blow picture the table view cells overlap with the segmented control:
By debugging the view hierarchy I found out that the problem is that the cells are displayed even if they are outside of the container view:
What you see in the above pictures are the table view cells and the container view. I've drawn two red lines, one starting from the container view top and one starting from the top of the first table view cell and like you see the table view cells go beyond the bounds of the container view, which means that they are visible even if they are outside of the table view area. Instead, the container view which embeds the table view is below the segmented control and the separation space imposed by the constraints is respected.
Can you try setup clipsToBounds? It can be help.
tableView.clipsToBounds = true
Must be something to do with how you've laid out your constraints. Hard to tell by the pictures.
I set a height constraint to the view that has the segmented control. And set the vertical spacing between that view and the container view to 0.
I uploaded a demo project here.

How to constrain static table view controller cells to Safe Area in Swift/iOS

I have a table view controller with static cells. I’m running on iPhone X and when scrolling, the cells appear to be behind the navigation bar and unsafe area. What is preferred would be a solid orange color in the unsafe area instead of the image presented below. Is this possible? I can’t move to a table view inside of a view controller, which I would then constrain to the safe area, because my cells are static.
You need to use a Container View, so add a View Controller in your storyboard and put a Container View in it-by adding the container view it will automatically create a segue and a view controller delete both.
Now from your Container View create a segue to the Table View Controller and choose Embed.
Last step is to set the constraints for your Container View, the most important for your issue is the top one, set it 0 to the safe area.

How to create Layout with UIImageView and UICollectionView and collection view changes height on scroll?

I would like to create such a layout where on scroll the imageview hides and collection view takes whole screen. I tried this but it doesnt seem to work. I am getting the image view correctly but the collection view has gone dark completely on run. I have a content view(UIView) and scroll View underneath. Thank you
EDIT 1 :- The Cells are not getting displayed. But the collection view is displayed in the layout. How to solve this?
EDIT 2 :- Instead of using the collection view directly I used a container view and embedded my collection view in it and it Worked! However I am still trying to figure out how do i scroll the container view to top when i scroll, i.e. push the image view on the back of container view while it takes the full height of the screen.
Okay So I figured it out.
Wreck all of the above. (Though the container view solution is pretty good)
Build a collection View controller from scratch having 2 headers, one is the standard and the other one for the image view.
Make your view controller a delegate of the collection view flow layout delegate and implement function for referenceSizeForHeaderInSection
Do not forget to register for the xib yo have created for the image view header by using registerNib forSupplementaryViewOfKind function of collection view object.

Container View not visible on view, but inside the TableView

I'm trying to add my custom ContainerView to an usual ViewController using StoryBoard (iOS7).
I only have a TableView Controller on this view. If I move the ContainerView inside the TableView it is displayed (but moved with the table items), but if I place it directly on view it is not displayed any more! Any ideas?
There appears to be a bug in Xcode/iOS where if the container view is the first child of self.view in storyboard, it doesn't show. For example when I have:
my container doesn't show.
But if I move the label above the container view in the outline:
Then my container view and the label will show.
UPDATE: Upon further investigation it appears the container view does still show if it is the first child, just much lower than you would expect it to appear. Placing another element above the container view allows the container to appear in the position you would expect (the position shown in storyboard).

Resources