为什么更改UIScrollView的框架会更改其子视图的框架?

sol*_*sol 3 iphone positioning uiscrollview ipad

这让我很困惑.我希望在方向更改时更改UIScrollView的框架:

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);
            }
        }
Run Code Online (Sandbox Code Playgroud)

结果如下.请注意,子视图向左移动了156个像素,即使我所做的只是更改父滚动视图的宽度(纵向模式下小256像素):

    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
Run Code Online (Sandbox Code Playgroud)

为什么??我该如何防止这种情况?

Nim*_*rod 12

您可能在子视图上设置了错误的autorisize标志.您还可以禁用UIScrollView autoresizesSubviews(在接口构建器中或手动)以禁用此整个行为.