简体   繁体   中英

Change UICollectionViewCell layout on orientation

I'm developing an app in which I have a UICollectionView. The UICollectionView has different types of UICollectionViewCells in it. For one of the Cell, I need to have different layout for different orientation.

In portrait view the cell should have the following layout

在此输入图像描述

For landscape I need this layout

在此输入图像描述

What is the best way to do this? Also, I have to make this work on iOS 7 devices as well. Please help.

Thanks

This will be my logic:

Add one more collectionViewCell For the landscape layout with a different reuseIdentifier.

Declare a BOOL variable like : BOOL isLAndscape;

in the implementation section.By default the bool value is NO or False .

Then check for the Notification if Orientation Changed! You Can set This Thing in viewDidLoad like

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

and whenever Orientation of your Device changed OrientationDidChange Called where You can do whatever You Want as Per Orientation.In your case ,if the orientation is landscape,then isLAndacape=YES; and then reload the 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];
    }
  }

And in the cellForItemAtIndexPath method of the collectionView,add the condition check for the bool islAndacape and change the reuseIdentifier.

NSString *reuseIdentifier;
if(isLAndscape)
{
reuseIdentifier=@"landscapeReuseId";
}
else
{
reuseIdentifier=@"portraitReuseId";
}

Thats it!!

DISCLAIMER!!! this is my logic.I have not tried this.Check if it works.

您需要在横向旋转上更改collectionViewLayout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM