tl;dr I don't want increases in the sizes of UIViews
above the top of the UIScrollView's
content window to push the content below it downwards.
I have a UIScrollView
that displays a list of many UIStackViews
, and one of the benefits of UIStackViews
is that when you set isHidden = false
to one of its subviews, it resizes automatically to compensate for the new view.
Now, each list item has one of these UIStackViews
, and the user can press a button that sets isHidden = false
to a subview in each one, all at once. That means, each list item will increase by an amount equal to the height of the unhidden view (the same height for each stack view).
There is no problem if the scrollview's contentOffset
is zero, because all the increases in height will just push all the rest of the content downwards, and the user will still be at the top of the scrollview after all the views have unhidden.
The problem is when the user is not at the top of the scrollview. When this is the case, and the user presses the button that unhides all the views, the increases in height will push all the views downwards, giving the appearance of scrolling upwards. The further down the user is scrolled in the list, the more views above it, and the greater the content shift when the user presses the button.
The behavior I'm looking for is the same as when the user presses the button when they are scrolled to the top of the view, but at any scroll position. In other words: regardless of where the user is scrolled too, when they press the button, the only views that are pushed down are the ones below the top of the scrollview's content window.
Any idea how to accomplish this?
1 Answers
Answers 1
Okay, so let me see if I understand your question properly.
This is the state of the Scroll View
A B C D E
When the user presses the "expand all" button, it looks like this:
A -1 -2 -3 B -1 +-a +-b -2 C -1 -2 -3 D -1 -2 E -1 -2 -3 -4
The problem is that if the user is currently looking at
+---+ |D | |E | +---+
(which are the 4th and 5th items in the list) they end up seeing in the expanded list:
+---+ |B | |-1 | +---+
But they should be seeing items D
that they were looking at prior.
I would probably recommend using a UITableView
as you can store the currently visible TableViewCells and then as the view animates, scroll to the current cell (which should keep it relatively in-view).
In your case, with the UIScrollView
what I would recommend is capturing the current visible subviews (using current contentOffset
/Size
and the frames of the subviews), then figuring out where they are after the view animates it's size transition, and then scroll to the new position.
This is, as I mentioned, a task better suited to UITableView
or UICollectionView
where the parent objects are tracked and cached by the OS and there are specific rules governing the insertion/deletion of rows/cells.
HtH
0 comments:
Post a Comment