Author Topic: How to correctly resize an UIScrollView to fit screen?  (Read 7109 times)

quietpixel

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
How to correctly resize an UIScrollView to fit screen?
« on: March 19, 2014, 04:29:20 PM »
What is the correct way to resize or scale a UIScrollView to account for different aspect ratio screen sizes?

I have an app that uses a scroll view to select game levels. I have it laid out correctly for an iPad screen. It works fine on all screens of that same aspect ratio, i.e. iPad 1-4, retina and non-retina -- because the NGUI camera scales the whole GUI to fit the screen.

But when I go to an iPhone, iPhone 5, or Android screen, the aspect ratios are different. This results in the scroll view either not filling up all the available real estate, or going offscreen. I would like to resize the scroll view so it fits as snuggly as it does on the iPad.

I have spent many days trying to come up with the correct mathematical formula to calculate the updated settings for the enclosing UIPane's clipping rectangle and its XY offset, and also the UIScrollView's drag percent, reset position, etc. I have given up and hardcoded values for each expected screen size, which is really ugly and fragile code.

Does anyone know the magic formula?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to correctly resize an UIScrollView to fit screen?
« Reply #1 on: March 20, 2014, 01:15:03 AM »
Why don't you just use the anchoring system to do this for you? Enable anchors on the scroll view's panel and anchor it to the sides of the screen.

quietpixel

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: How to correctly resize an UIScrollView to fit screen?
« Reply #2 on: March 20, 2014, 03:07:36 PM »
Thank you, ArenMook, that solved the problem for me, without having to try calculating the correct values myself.  I did not know this feature had been added to the most recent versions of NGUI.

A very nice upgrade!

Is there an easy way to make the anchor distance be a certain percentage of screen width or height?  I am able to change the anchor distance via code to maintain a nice gap on the sides on all screen sizes, but am curious if there is a way to do this automatically?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to correctly resize an UIScrollView to fit screen?
« Reply #3 on: March 21, 2014, 03:14:51 PM »
"Custom" anchoring. Check the documentation page for UIRect.

renegrox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: How to correctly resize an UIScrollView to fit screen?
« Reply #4 on: July 28, 2014, 09:46:12 AM »
Hello, I have a similar problem, I anchored the panel to the sides of the screen, but the children do not have anchors, if I anchor children to the panel, then I can not move them. if they are not anchored to anything, they can not adapt to screen size, the children need to adapt to the screen size. I'm using containers as children, because they are items that contain one image, text and one button.
Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to correctly resize an UIScrollView to fit screen?
« Reply #5 on: July 29, 2014, 03:15:58 AM »
Use advanced anchoring, and anchor sides that don't have to scroll. For example, in a horizontal scroll view you can anchor Top and Bottom of the items within.