在旋转时调整 UICollectionViewFlowLayout 单元格
Adjusting UICollectionViewFlowLayout cells on rotation
所以我正在开发一个属于 ePub 框架的应用程序。该应用程序将 reader 嵌入到其视图中,并允许用户通过滑动滚动缩放和翻页。我试图修改它,以便 ePub 列表作为显示 epub 封面图像的单元格进入集合视图。我可以为 flowlayout 创建单元格并使用框架边界我可以创建单元格但是当我尝试从纵向旋转到横向时它们不会调整大小和位置。我的单元格代码是这样的:
//Set Collection View Cell Size and Orientation
-(CGSize)
collectionView:(UICollectionView *) collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//Set Landscape size of cells
if(UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){
CGFloat cellWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat cellHeigt = [[UIScreen mainScreen] bounds].size.height-350;
NSLog(@"Is Landscape");
return CGSizeMake(cellWidth, cellHeigt);
}
//Set Potrait size of cells
else{
NSLog(@"Is Portrait");
CGFloat cellWidth = [[UIScreen mainScreen] bounds].size.width-80;
CGFloat cellHeigt = [[UIScreen mainScreen] bounds].size.height-240;
return CGSizeMake(cellWidth, cellHeigt);
}
}
我使用这段代码来定位它们:
//Collection View Cell Position
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
if(UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)){
return UIEdgeInsetsMake(0,10,0,20); // top, left, bottom, right
}
else{
return UIEdgeInsetsMake(0,20,0,20); // top, left, bottom, right
}
}
当我旋转设备时,单元格顶部边界越过框架。
请帮忙,我怎样才能动态更新框架?
旋转时需要刷新collection。试试下面的代码:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[yourCollectionview performBatchUpdates:nil completion:nil];
}
所以我正在开发一个属于 ePub 框架的应用程序。该应用程序将 reader 嵌入到其视图中,并允许用户通过滑动滚动缩放和翻页。我试图修改它,以便 ePub 列表作为显示 epub 封面图像的单元格进入集合视图。我可以为 flowlayout 创建单元格并使用框架边界我可以创建单元格但是当我尝试从纵向旋转到横向时它们不会调整大小和位置。我的单元格代码是这样的:
//Set Collection View Cell Size and Orientation
-(CGSize)
collectionView:(UICollectionView *) collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//Set Landscape size of cells
if(UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){
CGFloat cellWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat cellHeigt = [[UIScreen mainScreen] bounds].size.height-350;
NSLog(@"Is Landscape");
return CGSizeMake(cellWidth, cellHeigt);
}
//Set Potrait size of cells
else{
NSLog(@"Is Portrait");
CGFloat cellWidth = [[UIScreen mainScreen] bounds].size.width-80;
CGFloat cellHeigt = [[UIScreen mainScreen] bounds].size.height-240;
return CGSizeMake(cellWidth, cellHeigt);
}
}
我使用这段代码来定位它们:
//Collection View Cell Position
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
if(UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)){
return UIEdgeInsetsMake(0,10,0,20); // top, left, bottom, right
}
else{
return UIEdgeInsetsMake(0,20,0,20); // top, left, bottom, right
}
}
当我旋转设备时,单元格顶部边界越过框架。
请帮忙,我怎样才能动态更新框架?
旋转时需要刷新collection。试试下面的代码:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[yourCollectionview performBatchUpdates:nil completion:nil];
}