2014-05-19 4 views
6

collectionView의 항목 크기에 문제가 있습니다. 세로 모드의 경우 행에 3 개의 항목을 표시하고 가로 모드의 경우에는 6 개의 항목을 표시하려고합니다. 나는이 같은 -layoutSubviews을 설정 한 :UICollectionView 레이아웃 업데이트 - 항목 크기가 변경되지 않습니다.

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)) { 
     //Portrait orientation 
     self.flowLayoutPortrait.itemSize = CGSizeMake(106, 106); 

    } else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)) { 
     //Landscape orientation 
     self.flowLayoutPortrait.itemSize = CGSizeMake(93, 93); 
    } 
    [self.collectionView.collectionViewLayout invalidateLayout]; 
} 

그러나 셀 크기는 항상 두 방향에 대해 동일하게 유지, 업데이트되지 않습니다. 나는 2 flowLayouts 및 사용을 작성하는 경우 :

[self.collectionView setCollectionViewLayout:self.flowLayoutLandscape]; 

모든 작품을,하지만 난 정말 그들이 변경하는 방법을 좋아하지 않는다. 애니메이션이 정말 안좋아. 그리고 itemSize가 업데이트해야하는 유일한 속성이므로 1 레이아웃을 사용하는 것이 가장 좋은 옵션처럼 보입니다.

어떻게하면 레이아웃을 업데이트하도록 collectionView에 알릴 수 있습니까?

답변

12

나는이 방법을 사용이 아주 좋은 날이었다

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (_isLandscape) 
     return CGSizeMake(yourLandscapeWidth, yourLandscapeHeight); 
    else 
     return CGSizeMake(yourNonLandscapeWidth, yourNonLandscapeHeight); 
} 
+7

감사합니다,이 위대한 작품! 미래의 독자를 위해서'willRotateToInterfaceOrientation :'에'[collectionView.collectionViewLayout invalidateLayout]'을 추가 할 필요가있다. –

0

다른 식별자가있는 세로/가로 모드에 다른 셀을 사용할 수 있습니다. 컬렉션보기에서 데이터를 다시로드하기 만하면되는 것보다 확실히 작동 할 것입니다.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)) { 
     //Portrait orientation 
     //dequeue cell for portrait mode 

    } else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)) { 
     //Landscape orientation 
     //dequeue cell for landscape mode 
    } 

    return cell; 
}