Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationController Large Title and multiple UIScrollViews/UITableViews

I'm looking for a solution to be able to host multiple scrollviews/tableviews in a viewcontroller that is embedded inside a navigation controller that uses large titles.

The idea is to have a Segmented Control with two/three segments which will show/hide tableviews and/or other scrollviews.

My issue is that in order for the Large Titles to work (i.e. expand/collapse as the tableview/scrollview is scrolled up & down) it expects that the first view is the tableview/scrollview that is being scrolled. This works well for my initial tableview, but once I navigate (which is done by simply hiding/showing another tableview) to the second/third, it stops working (because the subsequent tableviews are not first in the view hierarchy). Here an example:

Storyboard Example

As can be seen from the screenshot above I have created two scrollviews that are controlled by a segmented control. If I simply show/hide them, there will be no large title collapse on the Second scrollview as, like mentioned above, UINavigationController always expected the scrollable view to be the first in the view hierarchy for this to work.

To get closer to what I need I'm also sending the subview that is about to be displayed 'back':

self.view.sendSubviewToBack(secondScrollView)

This alone hasn't worked unfortunately, as it seems that although the new scrollview is the first item in the view hierarchy, UINavigationController still remembers the 'old' hierarchy. I have managed to 'trick' the UINavigationController to refresh and recognise the new hierarchy with the bellow code:

self.navigationController?.navigationBar.prefersLargeTitles = false
self.navigationController?.navigationBar.prefersLargeTitles = true

Which seems to finally make the Large Titles to shrink and expand when needed, however, a new problem has been introduced. When navigating to the second scrollview, it's initial state is collapsed instead of being expanded:

Collapsed second scrollview

If I try scrolling up & down the problem immediately fixes itself but I would rather make it work first time. Here is my ViewController code:

view controller code

Basically I have been struggling with this for a while and I'm out of ideas... Ideally I would like a clean solution (i.e. turning on and off prefersLargeTitles to make this work seems very hacky!) but also making the current solution work will also do :-)

I have considered other solutions like having a single top UIScrollView and switching content from within (i.e. either adding/removing views or showing/hiding/ or even embedding a container view) however, as I will need to work with tables too, I don't think it is a good idea to embed a table in a scrollview (as a table itself has a scrollview for it's content).

like image 577
nonstopcoding Avatar asked Nov 18 '25 13:11

nonstopcoding


1 Answers

You need to call navigationBar.sizeToFit() after you toggle the largeTitle on/off.

navigationController?.navigationBar.prefersLargeTitles = false
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.sizeToFit()
like image 139
timcmiller Avatar answered Nov 20 '25 03:11

timcmiller