简体   繁体   English

在方向上更改UICollectionViewCell布局

[英]Change UICollectionViewCell layout on orientation

I'm developing an app in which I have a UICollectionView. 我正在开发一个应用程序,其中我有一个UICollectionView。 The UICollectionView has different types of UICollectionViewCells in it. UICollectionView中包含不同类型的UICollectionViewCell。 For one of the Cell, I need to have different layout for different orientation. 对于其中一个Cell,我需要为不同的方向设置不同的布局。

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. 此外,我还必须在iOS 7设备上完成这项工作。 Please help. 请帮忙。

Thanks 谢谢

This will be my logic: 这将是我的逻辑:

Add one more collectionViewCell For the landscape layout with a different reuseIdentifier. 添加一个collectionViewCell对于具有不同reuseIdentifier的横向布局。

Declare a BOOL variable like : BOOL isLAndscape; 声明一个BOOL变量,如: BOOL isLAndscape;

in the implementation section.By default the bool value is NO or False . 在实现部分。默认情况下,bool值为NOFalse

Then check for the Notification if Orientation Changed! 如果方向改变,请检查通知! You Can set This Thing in viewDidLoad like 你可以在viewDidLoad中设置This Thing

[[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; 每当你的设备的方向改变OrientationDidChange调用你可以做任何你想要的每个方向。在你的情况下,如果方向是横向,那么isLAndacape = YES; and then reload the collectionView. 然后重新加载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. 在collectionView的cellForItemAtIndexPath方法中,添加bool islAndacape的条件检查并更改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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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