views:

38

answers:

2

This is perplexing to me. I want to change the frame of a UIScrollView upon orientation change:

if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){

            self.myScrollView.frame = CGRectMake(40, 40, 984, 200);
            for (UIView* view in [self.myScrollView subviews]){
                NSLog(@"subview x: %f", view.frame.origin.x);
            }

        }
        else{

            self.myScrollView.frame = CGRectMake(40, 40, 728, 200);
            for (UIView* view in [self.myScrollView subviews]){
                NSLog(@"subview x portrait: %f", view.frame.origin.x);
            }
        }

Here are the results. Note that the subviews are all moved 156 pixels to the left, even though all I did was change the width of the parent scroll view (256 px smaller in portrait mode):

    subview x: 0.000000
    subview x: 128.000000
    subview x: 256.000000
    subview x: 384.000000

    subview x portrait: -156.000000
    subview x portrait: -28.000000
    subview x portrait: 100.000000
    subview x portrait: 228.000000

Why?? And how do I prevent this?

+1  A: 

You probably have the wrong autorisize flags set on the subviews. You can also disable UIScrollView autoresizesSubviews (ininterface builder or manually) to just disable this whole behavior.

Nimrod
myScrollView.autoresizesSubviews = NO; does the trick. thanks.
sol
no prob. there's also a checkbox to disable it in interface builder
Nimrod
A: 

maybe setting this chould help?

myScrollview.resizesSubviews = NO;

or if you subclassing uiscrollview you could implement layoutSubviews to customize your layout for a certain frame size ;)

nacho4d