Author Topic: Dynamic ScrollView and hidden tabs  (Read 2070 times)

Livealot

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 17
    • View Profile
Dynamic ScrollView and hidden tabs
« on: May 12, 2014, 09:23:17 PM »
I have created a menu based on Example 13-Tabs.

I have created a Scrollview on each Tab, with a Grid that is populated at runtime using  NGUITools.AddChild(MyGrid, MyPrefab).

I call MyGrid.GetComponent<UIGrid>().Reposition();  after populating each of the grids.

And I call MyGrid.transform.parent.GetComponent<UIScrollView>().ResetPosition(); for each grid on each tab after that.

This works perfectly for the tab that starts as active.  But the non-active tab does not reset correctly.  The content on the hidden tab starts in the middle of the scrollview rather than the TopLeft. (see screenshot attached).

Any tips on where/when to call ScrollView.ResetPosition on the hidden tab?

I tried calling a specific refresh function from the OnValueChange of the UIToggle script attached to the tab handle which activates the hidden tab, but that didn't seem to fire fast enough.

Of course, once you interact with the content on the hidden tab, everything snaps/works perfectly.  Just not at load time of the scene.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dynamic ScrollView and hidden tabs
« Reply #1 on: May 13, 2014, 08:35:16 AM »
Note the description of UIScrollView.ResetPosition:
  1.         /// <summary>
  2.         /// Reset the scroll view's position to the top-left corner.
  3.         /// It's recommended to call this function before AND after you re-populate the scroll view's contents (ex: switching window tabs).
  4.         /// Another option is to populate the scroll view's contents, reset its position, then call this function to reposition the clipping.
  5.         /// </summary>
Call it once, repopulate your content, call it again. You can also try calling UpdatePosition(). It's generally easier to have multiple scroll views rather than populating the same one.

Livealot

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: Dynamic ScrollView and hidden tabs
« Reply #2 on: May 13, 2014, 10:21:57 AM »
My issue was finding the right spot to call the UIScrollView.ResetPosition.  Before and after populating the contents was not sufficient.  Triggering from the UIToggle of the handle was not fast enough.

After some more trial and error, I found a method that seems to work.

I created a helper script on each ScrollView.  I have one for each tab.  That script uses OnEnable to both Reposition the UIGrid child of the ScrollView and then ResetPosition of the ScrollView itself.  For example,

void OnEnable() {
      //print ("a scrollview was just enabled for " + this.name);
      this.GetComponentInChildren<UIGrid>().Reposition();
      this.GetComponent<UIScrollView>().ResetPosition();
   }

Hope this helps folks since it's not a step I would have thought of in setting up ScrollView.
« Last Edit: July 08, 2014, 10:04:52 PM by Livealot »