Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: EToreo on November 26, 2013, 06:36:55 PM

Title: UITable/UIScrollView Adjust position
Post by: EToreo on November 26, 2013, 06:36:55 PM
I have a table and a scroll view that allows the player to vertically scroll though the cells in the table.  The player can swap out the entire content of that table by pressing a button.

When the content is swapped out, it is not "vertically aligned" to the top of the table - it starts somewhere near the center (it seems to have something to do with the amount of content that was there before and it's position before the swap happens).

Here is the code I am using to do this, what can I do to get the content to have the first cell aligned to the top of the visible table?
(The "rand" is a contrived example of the content being dynamic in size)
  1.  
  2.    for(int i = 0; i < Table.transform.childCount; i++)
  3.       Destroy(Table.transform.GetChild(i).gameObject);
  4.      
  5.    for(int i = 0; i < rand(5, 10); i++)
  6.    {
  7.       GameObject pNewGo = NGUITools.AddChild(Table.gameObject, Resources.Load("Prefabs/MainMenu/OptionsCellPrefab") as GameObject);
  8.       pNewGo.name = val;
  9.       pNewGo.transform.position = Vector3.zero;
  10.       pNewGo.transform.localScale = Vector3.one;
  11.       pNewGo.transform.localPosition = Vector3.zero;
  12.    }
  13.    Table.Reposition();
  14.  
  15.  
Title: Re: UITable/UIScrollView Adjust position
Post by: ArenMook on November 26, 2013, 11:42:16 PM
Are you ever resetting the scroll view's position? You need to do it by calling UIScrollView.ResetPosition (http://www.tasharen.com/ngui/docs/class_u_i_scroll_view.html#ac4fbddd1de781f66473614694ea78a91), ideally before and after you re-populate your table.
Title: Re: UITable/UIScrollView Adjust position
Post by: macy on November 26, 2013, 11:53:43 PM
Table.Reposition();
Reposition all the items in the UITable.

If you call Table.Reposition() at the Start, all the items starts somewhere near the centre, then it means your UITable placed at that position and everything will be placed there. Please make sure the UITable is placed at the Top Left at first.

If that doesn't work, you can call the UIDraggablePanel.repositionClipping to reClipping the clipping range.
or

call the scrollbar, to set the value to 0.

plus Destroy doesn't work right the way, please try DestroyImmediate. If you do a count at your script, you may get more items in the Table than it looks.
Title: Re: UITable/UIScrollView Adjust position
Post by: EToreo on November 27, 2013, 12:55:13 AM
UIScrollView.ResetPosition() alone doesn't appear to do the trick... in all three cobinations of before and/or after.

As for Marcy's suggestions DestroyImmediate has unintended consequences and based on the documentation that makes sense.
Title: Re: UITable/UIScrollView Adjust position
Post by: EToreo on December 06, 2013, 04:09:57 AM
On the 3.0.7 notes you say:
Quote
- NEW: You can now create resizable scroll views and anchor them to UI elements.

Might this fix my problem?
Title: Re: UITable/UIScrollView Adjust position
Post by: ArenMook on December 06, 2013, 04:11:36 AM
The new layout system fixes quite a few problems, so likely.