おそらく、スクロールビューのサイズに基づいてスクロールビューと画像(または私の場合は画像付きのボタン)を調整するルーチンが必要です(そしてautoSizingMask
、方向が変わると伸びるようにスクロールビューを設定してください)。
したがって、たとえば、次のルーチンがあります(アイコンごとにUIButtonがすでに追加されたスクロールビューが作成されていることを前提としています...この基本的な考え方は、UIImageViewsを使用している場合にも機能します)。
- (void)rearrangeImages
{
if (!_listOfImages)
{
[self loadImages];
return;
}
// a few varibles to keep track of where I am
int const imageWidth = [self thumbnailSize];
int const imagesPerRow = self.view.frame.size.width / (imageWidth + 2);
int const imageHeight = imageWidth;
int const imagePadding = (self.view.frame.size.width - imageWidth*imagesPerRow) / (imagesPerRow + 1);
int const cellWidth = imageWidth + imagePadding;
int const cellHeight = imageHeight + imagePadding;
NSInteger row;
NSInteger column;
NSInteger index;
CGRect newFrame;
// iterate through the buttons
for (UIView *button in [_scrollView subviews])
{
index = [button tag];
if ([button isKindOfClass:[UIButton class]] && index < [_listOfImages count])
{
// figure out where the button should go
row = floor(index / imagesPerRow);
column = index % imagesPerRow;
newFrame = CGRectMake(column * cellWidth + imagePadding,
row * cellHeight,
imageWidth,
imageHeight);
if (button.frame.origin.x != newFrame.origin.x || button.frame.origin.y != newFrame.origin.y)
[button setFrame:newFrame];
}
}
NSInteger numberOfRows = floor(([_listOfImages count] - 1) / imagesPerRow) + 1;
[_scrollView setContentSize:CGSizeMake(self.view.frame.size.width, numberOfRows * cellHeight)];
}
次に、画面の向きが変わったときにアプリにこれを呼び出させます。
- (void)viewWillLayoutSubviews
{
[self rearrangeImages];
}
iOS 5より前のバージョンをサポートしている場合は、次のようなものも必要になる場合があります。
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
float iOSversion = [[[UIDevice currentDevice] systemVersion] floatValue];
// we don't need to do this in iOS 5, because viewWillLayoutSubviews is automatically called
if (iOSversion < 5.0)
[self viewWillLayoutSubviews];
}