0
votes

I have 3 different views and view controllers. 1 is responsible for displaying some review text, it has a UIScrollView and a UILabel. Another is responsible for displaying comments, it has a UIScrollView a nested UIStackView. The third one, I want to combine the previous 2 and be able to scroll them them together, and it has a UIScrollView (the contained element at this stage can be anything).

I want to keep this separation because the review text will be displayed elsewhere, by itself or with other information as well and I've abstracted the comments so that the same code can be used with a different comment model to display different comments depending on the context; with this particular context being a discussion on the review text.

The end result I'm looking for is that review text and the comments can be scrolled together.

Naively, I've tried adding the view controllers as children and the views to a UIStackView within the UIScrollView and I ended up with two views that scroll independently, more or less. Which makes sense, but is not what I want. I'm finishing testing using UICollectionView and it doesn't seem to work (the child view controllers don't seem to get notified when the view appears, which is where I'm setting the UILabel's text for the review and building the nested UIStackViews for the comments). My next idea is to somehow 'rip' the content out of the views' scroll views and place it into the third's UIScrollView's container (probably a UIStackView). But before I do that, I wanted to see if there was an easier way to effectively merge these 2 scroll views (and their associated view controllers) into a third, containing scroll view (and its view controller).

Blockquote

1
a mockup illustrating what you're trying to achieve would be extremely helpfulJason
There are various ways you could do this. One approach would be the first thing you tried: adding the VCs as children and adding their views to a UIStackView. The key is to set the height of the "child scrollViews" to the height of their contents. That would stop them from scrolling independently.DonMag
I've tried changing the comment's scroll view's frame height to 5000 (for quick dirty testing) and it has no effect.MendAndDefend
@MendAndDefend Hi , you can share some sample code to explain what you have tried .And explain where code not get what needs . That will be helpful to solve this problem .Junior Jiang
After further testing, @DonMag is right. It wasn't working for me because I had to set the 'TranslatesAutoresizingMaskIntoConstraints' flag. So, if you'd care to make your comment an answer, I'll select it.MendAndDefend

1 Answers

0
votes

As per comments from the OP...

You can add the VCs as child view controllers, and then add their views to a UIStackView.

The key to getting that to work is to set the height of the child views (and their scroll views) to match the height of the scroll views' content.

Note that the "root view" of a VC loaded as a child will have .translatesAutoresizingMaskIntoConstraints = true ... so if you're going to use auto-layout constraints, remember to set it to false.