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

时间:2010-10-14 18:21:51

标签: iphone ipad uiscrollview positioning

这令我感到困惑。我想在方向改变时更改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);
            }
        }

以下是结果。请注意,子视图向左移动了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

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

2 个答案:

答案 0 :(得分:12)

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

答案 1 :(得分:0)

也许设置这个chould帮助?

myScrollview.resizesSubviews = NO;

或者如果您继承uiscrollview,您可以实现layoutSubviews来自定义特定帧大小的布局;)