3

我正在開發一個應用程序,其中有一個UICollectionView。 UICollectionView在其中有不同類型的UICollectionViewCells。對於單元格之一,我需要針對不同的方向有不同的佈局。在方向上更改UICollectionViewCell佈局

在縱向視圖中的細胞應具有以下佈局

enter image description here

對於風景,我需要這個佈局

enter image description here

什麼是做到這一點的最好方法是什麼?另外,我也必須在iOS 7設備上進行這項工作。請幫忙。

感謝

+0

你有兩個這兩個佈局的collectionViewCells? – abhi1992

+0

現在,沒有。但我可以補充一點。如何在方向上加載新佈局? – avrospirit

回答

1

這將是我的邏輯:

再添加一個collectionViewCell對於具有不同reuseIdentifier景觀佈局。

聲明像布爾變量:在實現section.By默認bool值 BOOL isLAndscape;

NOFalse或。

然後檢查方向是否改變! 你可以設置這個東西在viewDidLoad中像

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

,每當你的設備的方向改變OrientationDidChange調用,你可以做任何你想做的每Orientation.In你的情況下,如果方向爲橫向,然後isLAndacape = YES;然後重新加載collectionView。

-(void)OrientationDidChange:(NSNotification*)notification 
{ 
    UIDeviceOrientation Orientation=[[UIDevice currentDevice]orientation]; 

    if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight) 
    { 
    isLAndscape=YES; 
    [collectionVIew reloadData]; 
    } 
    else if(Orientation==UIDeviceOrientationPortrait) 
    { 
    isLAndscape=NO; 
    [collectionVIew reloadData]; 
    } 
    } 

而在的CollectionView的cellForItemAtIndexPath方法,添加條件檢查的布爾islAndacape並更改reuseIdentifier。

NSString *reuseIdentifier; 
if(isLAndscape) 
{ 
[email protected]"landscapeReuseId"; 
} 
else 
{ 
[email protected]"portraitReuseId"; 
} 

這就是它!

免責聲明!這是我的邏輯。我沒有嘗試過,檢查它是否有效。

+0

謝謝@ abhi1992。這樣可行。但重新加載UICollectionView定位一個很好的選擇? – avrospirit

+0

或者您應該在運行時添加約束條件以適應佈局,對於我來說這是一個有點複雜的代碼。我實際上不知道.Reloading collectionview是一個簡單的代碼,而不是一個複雜的東西。 – abhi1992

0

您需要更改collectionViewLayout上橫向旋轉

+0

謝謝。你能在這裏粘貼一個示例代碼嗎? – avrospirit