sol
sol

Reputation: 6462

Why does changing the frame of a UIScrollView change the frames of its subviews?

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?

Upvotes: 3

Views: 5789

Answers (2)

nacho4d
nacho4d

Reputation: 45158

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 ;)

Upvotes: 0

Nimrod
Nimrod

Reputation: 5133

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.

Upvotes: 12

Related Questions