繁体   English   中英

在方向上更改UICollectionViewCell布局

[英]Change UICollectionViewCell layout on orientation

我正在开发一个应用程序,其中我有一个UICollectionView。 UICollectionView中包含不同类型的UICollectionViewCell。 对于其中一个Cell,我需要为不同的方向设置不同的布局。

在纵向视图中,单元格应具有以下布局

在此输入图像描述

对于风景我需要这种布局

在此输入图像描述

做这个的最好方式是什么? 此外,我还必须在iOS 7设备上完成这项工作。 请帮忙。

谢谢

这将是我的逻辑:

添加一个collectionViewCell对于具有不同reuseIdentifier的横向布局。

声明一个BOOL变量,如: BOOL isLAndscape;

在实现部分。默认情况下,bool值为NOFalse

如果方向改变,请检查通知! 你可以在viewDidLoad中设置This Thing

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

每当你的设备的方向改变OrientationDidChange调用你可以做任何你想要的每个方向。在你的情况下,如果方向是横向,那么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方法中,添加bool islAndacape的条件检查并更改reuseIdentifier。

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

而已!!

免责声明! 这是我的逻辑。我没试过这个。检查一下是否有效。

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

暂无
暂无

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

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