2012-07-09 4 views
1

나는과 같이 내 응용 프로그램의 항목 그리드를 가지고 :회전시 격자 스타일 요소를 자동 정렬하는 가장 좋은 방법은 무엇입니까?

http://cl.ly/1m243117220B060Z0M26/Screen%20Shot%202012-07-09%20at%2011.34.22%20AM.png

이 순간에 단지 지정 UIButtons 있습니다. 가로 모드의 너비에 맞게이 격자 항목을 자동으로 다시 배열하는 가장 좋은 방법은 무엇입니까?

http://cl.ly/1d0x431P1n211W1H2n3B/Screen%20Shot%202012-07-09%20at%2011.35.42%20AM.png

은 분명히이 앱에서 일반적으로 수행하지만 난 조금 검색하고 내가 찾던 정확히 찾을 수 없습니다.

추가 보너스 누군가의 크기에 따라 iOS 용 동위 원소 같은 (http://isotope.metafizzy.co/)

+0

화면을 채우지 만 행당 3 개 항목을 유지하거나 그리드 크기를 변경 하시겠습니까? 세로 3 개, 가로 5 개를 세로로 말하십시오. – George

+0

수정 -> "초상화 3 행당 항목 및 가로 5 항목 행 당." – Dave

+0

내가 scrollview에 이미지를 넣을 때, 나는 "행"에 들어갈 수있는 숫자를 알아 내고 모든 이미지에 대해 프레임을 설정합니다 따라서. 그런 다음 오리엔테이션이 변경되고 컨트롤을 다시 배치해야 할 때마다 iOS 5에서 호출되는 'viewWillLayoutSubviews'에서이 내용을 다시 호출합니다. 예를 들어 나의 대답을보십시오. 이것은 분명히 내 애플 리케이션에 특정이지만, 그것이 작동하는 방법에 대한 기본적인 감각을 제공해야합니다. 옆으로, scrollview가 양쪽 방향에서 얼마나 넓게 있는지주의 깊게 생각하고 양쪽에서 멋지게 렌더링되는 이미지 크기를 선택하는 것이 좋습니다. – Rob

답변

1

당신은 아마 당신의있는 ScrollView 및 이미지를 조정하는 루틴을 원하는 (또는 내 경우에는, 이미지 버튼) 알고있는 경우 scrollview (스크롤 뷰의 autoSizingMask을 방향이 바뀔 때 늘어나도록 설정해야합니다).

예를 들어, 다음과 같은 루틴이 있습니다. UIButton이 이미 각 아이콘에 추가 된 상태에서 만든 scrollview가 있다고 가정합니다.이 기본 아이디어는 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)]; 
} 

그럼 예를 들어 내 응용 프로그램 호출이 화면이 변화 방향, 당신은 사전에서 iOS 5를 지원하는 경우

- (void)viewWillLayoutSubviews 
{ 
    [self rearrangeImages]; 
} 

, 당신은 또한 같은해야 할 수도 있습니다

- (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]; 
} 
관련 문제