这让我感到困惑。我想在屏幕方向改变时更改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);
}
}
以下是结果。请注意,尽管我只改变了父滚动视图的宽度(在纵向模式下减小了256像素),但所有子视图都向左移动了156像素:
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
为什么会出现这种情况?如何预防呢?