1
votes

In my code I am increasing height of the contentSize of a UIScrollView when the UI orientation changes to landscape in order all views fit there and the user can scroll to see it.

In my XIB I have

/UIView - view
  |- UIScrollView - scrollView
       |- other subviews that should be scrollable

The scrollView has the same size as the view and its autoresize mask is set to resize in all directions.

I noticed that the scrollView's height dimensions are much greater than view's height.

E.g. I have some debug logs:

NSLog(@"%@ - self.view.frame=%@, self.view.bounds=%@", NSStringFromSelector(_cmd), NSStringFromCGRect(self.view.frame), NSStringFromCGRect(self.view.bounds));
NSLog(@"self.scrollView.frame=%@, self.scrollView.bounds=%@", NSStringFromCGRect(self.scrollView.frame), NSStringFromCGRect(self.scrollView.bounds));

and it prints

willAnimateRotationToInterfaceOrientation:duration: - self.view.frame={{0, 0}, {480, 219}}, self.view.bounds={{0, 0}, {480, 219}}
self.scrollView.frame={{0, 0}, {480, 399}}, self.scrollView.bounds={{0, 0}, {480, 399}}

And even in the Xcode IB it looks like:

height of UIScrollView vs UIView

Why doesn't the UIScrollView have same size as the view?

Thanks


Update

The strange thing is that when I set height of the scrollView to 460 (iPhone portrait), Xcode/IB show that the scrollView is smaller than its parent view (which also has set the same dimensions): enter image description here

But now I have created a test project with 1 XIB that has only 1 UIView, added in there 1 UIScrollView and it looks normally, the UIScrollView's height matches the UIView's.

So perhaps I have somehow messed up the XIB and Xcode can't handle it well.

Anyway I am setting the dimensions programmatically (to handle rotations, UI composition changes etc.) and it works as expected. As it is my first iOS project, I was just wondering if there is any magic in UIScrollView. Apparently not :-)

Thanks for all your comments.

2
why don't you change the height of the scrollview to 460 ? - JeanLuc

2 Answers

1
votes

First of all, set the UIScrollView bounds to be 100% the same as its superview's. Next make sure that its superview has the autoresizesSubviews property set to YES. Then everything will work fine. Also "willAnimate..." means that the rotation operation has not yet been completed and you should check your new frames in the "didRotate" callback.

-1
votes

I have created a test project with 1 XIB that has only 1 UIView, added in there 1 UIScrollView and it looks normally, the UIScrollView's height matches the UIView's.

So perhaps I have somehow messed up the XIB and Xcode can't handle it well.

Thanks for the comments you gave me.