2013-07-29 91 views
0

我只是尝试创建我自己的布局。我使用UITableView作为我的UIViewController。我有一些来自服务器的JSON响应。这就像详细的出版物。另外我计算我的UITableViewCell高度,因为我的出版物包含图像和文字的混合。我编写了自己的布局,并在设备旋转时重新计算。如何正确布局

for (UIView * sub in contentSubviews) { 
    if([sub isKindOfClass:[PaddingLabel class]]) 
    { 
     PaddingLabel *textPart = (PaddingLabel *)sub; 
     reusableFrame = textPart.frame; 
     reusableFrame.origin.y = partHeight; 
     partHeight += textPart.frame.size.height; 
     [textPart setFrame:reusableFrame];    
    } else if([sub isKindOfClass:[UIImageView class]]) 
    { 
     UIImageView * imagePart = (UIImageView *)sub; 
     reusableFrame = imagePart.frame; 
     reusableFrame.origin.y = partHeight; 
     reusableFrame.origin.x = screenWidth/2 - imageSize.width/2; 
     reusableFrame.size.width = imageSize.width; 
     reusableFrame.size.height = imageSize.height; 
     [imagePart setFrame:reusableFrame]; 
     partHeight += imagePart.frame.size.height; 
    } 
} 

但我有一些问题。 当设备更改方向状态UIScrollView偏移量与以前相同时。我不知道如何改变它。 循环之前:

enter image description here

旋转后:

enter image description here

我想保存在矩形可见元素。 建议请。

回答

0

有几个选择,取决于你想旋转后显示什么。一种可能的解决方案是在旋转之前获取contentOffset.y/contentSize.height,然后将其倍增至新的contentSize.height以派生新的contentOffset.y。或者,如果您希望在旋转之前和之后保留特定元素,请获取element_view.frame.origin.y和contentOffset.y之间的区别,然后将新的contentOffset.y设置为element_view.frame.origin.y +区别。