在方向上更改 UICollectionViewCell 布局

Change UICollectionViewCell layout on orientation

我正在开发一个应用程序,其中有一个 UICollectionView。 UICollectionView 中有不同类型的 UICollectionViewCells。对于其中一个单元格,我需要针对不同的方向进行不同的布局。

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

对于风景,我需要这种布局

最好的方法是什么?此外,我还必须在 iOS 7 台设备上进行这项工作。请帮忙。

谢谢

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

这将是我的逻辑:

再添加一个collectionViewCell,用于横向布局,使用不同的reuseIdentifier。

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

在实现中section.By默认bool值是NOFalse.

然后检查方向改变时的通知! 你可以在 viewDidLoad 中设置 This Thing like

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

每当你的设备方向改变时 OrientationDidChange 调用你可以根据你的情况做任何你想做的事情 Orientation.In 你的情况,如果方向是横向,那么 isLANdacape=是的;然后重新加载 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";
}

就是这样!!

免责声明!!!这是我的 logic.I 没试过 this.Check 如果它有效。