Author Topic: UIScrollView Resize to Fit Content?  (Read 4373 times)

joeymaru

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
UIScrollView Resize to Fit Content?
« on: August 25, 2014, 12:04:00 AM »
Hey guys!

I have been fighting this issue for awhile now, scrounging the forums and stackoverflow for answers..

I am trying to get my UIScrollView to adjust its size depending on the content in a child table.
What I am doing right now is, I am attempting to use SetRect on the UIPanel, and then call ResetPosition on the scrollview but it is acting really strange.. Here is my code.

  1. public GameObject table;
  2. public GameObject friend;
  3. public GameObject scrollView;
  4. List<Transform> childList;
  5. public float scrollViewHeight;
  6. float childHeight;
  7. int numberOfChildren;
  8.  
  9. void Start() {
  10.         childHeight = friend.GetComponent<UIWidget>().localSize.y + 5;
  11.         childList = table.GetComponent<UITable>().children;
  12.         Invoke("Resize", 1f); // temp invoke for testing
  13. }
  14.  
  15. void Resize() {
  16.         numberOfChildren = childList.Count;
  17.         scrollViewHeight = childHeight * numberOfChildren;
  18.         UIPanel panel = scrollView.GetComponent<UIPanel>();
  19.         panel.SetRect(panel.clipOffset.x, panel.clipOffset.y, panel.width, scrollViewHeight);
  20.         scrollView.GetComponent<UIScrollView>().ResetPosition();
  21. }
  22.  

The Hierarchy looks like this:
ScrollView
    Table
        Child


Now there is probably a lot wrong going on up there, but I have been trying all kinds of things and so far this is the only thing that is remotely working..

I mostly just want the functionality of a native scrollview, when content is added or removed I want the scrollview to be exactly the size of the table, until it reaches screen height, of course..

I would love to hear some suggestions because I am losing my mind. :(

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIScrollView Resize to Fit Content?
« Reply #1 on: August 26, 2014, 01:38:40 AM »
First question that comes to mind is... why? Why keep the scroll view resized to match the contents? If you're worried about preventing the user from scrolling the scroll view, it's an option on the scroll view itself -- disable drag if fits.

UIPanel.SetRect adjusts the base clip region for you, but is this really what you want? If you've scrolled down, it won't reflect the change because it's the base clip region.

Also, why are you even using the UIWidget.localSize? Why not just use widget's height instead? There is a lot about what you're doing I don't quite understand.

joeymaru

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UIScrollView Resize to Fit Content?
« Reply #2 on: August 31, 2014, 12:15:24 AM »
Thanks for the reply, sorry it took my awhile to get back to you!

and oh my goodness, I feel so silly right now.
I looked at that option a million times I swear it and never once did it occur to me that is what I needed to turn on..........

I was trying to make everything super complicated for no reason. I was trying to resize the view and all of this other crazy pointless stuff when it was right in front of my face. ><;

Thank you so much! That option is what I needed...