简体   繁体   English

UICollectionViewCell相对于其集合视图的大小

[英]UICollectionViewCell sized relative to its collection-view

I have a (horizontal) collection-view, and I change its size via auto layout constraints. 我有一个(水平)集合视图,并且通过自动布局约束来更改其大小。 However, when the collection-view's size changes, the cell remains the same static size. 但是,当集合视图的大小更改时,单元格将保持相同的静态大小。

To demonstrate, here's the collection-view in the view debugger (I've labeled both the collectionview and one of its cells): 为了演示,这是视图调试器中的collection-view(我已经标记了collectionview及其单元格之一):

在此处输入图片说明

As you can see, the cell remains the original size and is now actually taller than its encasing collection-view. 如您所见,单元格保持原始大小,现在实际上比其包围的收集视图高。

So how can I constrain the cell to always fit within its collection-view? 那么,如何限制单元格始终适合其收藏视图呢?

If you want to resize your view you can use the following to reset the item size on rotation, this would go inside your view controller subclass. 如果要调整视图的大小,则可以使用以下命令在旋转时重置项目大小,这将放入视图控制器子类中。

This should be fairly efficent as it is only called when the size of the visible view controller changes. 这应该是相当有效的,因为只有在可见视图控制器的大小更改时才调用它。

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator;
{
  [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

  [coordinator animateAlongsideTransition:({
    ^(id<UIViewControllerTransitionCoordinatorContext> context) {

      UICollectionViewFlowLayout *layout = (id)self.collectionView.collectionViewLayout; {

        CGFloat height = CGRectGetHeight(self.collectionView.bounds);

        layout.itemSize = (CGSize) {
          .width  = height,
          .height = height
        };

      }

      [layout invalidateLayout];

    };
  }) completion:nil];

}

You should also check out these answers for some more solutions. 您还应该查看这些答案以获得更多解决方案。

I solved this through setting the item's height and width equal to the collection-view's height. 我通过将项目的高度和宽度设置为等于集合视图的高度来解决此问题。 I do this in sizeForItemAtIndexPath: 我在sizeForItemAtIndexPath中执行此操作:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(collectionView.frame.size.height, collectionView.frame.size.height);
}

Because in my case the cell's need to be perfect squares, setting the cell's height and width to the height of the collection-view works perfectly. 因为在我的情况下,单元需要是完美的正方形,所以将单元的高度和宽度设置为集合视图的高度是完美的。

Not that your class must implement the UICollectionViewDelegateFlowLayout protocol for this method to be exposed. 并不是您的类必须实现UICollectionViewDelegateFlowLayout协议才能公开此方法。

暂无
暂无

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

相关问题 集合视图中有多个Tableview - Multiple Tableviews inside Collection-view 如何在集合视图 swift 5 中更改动态行的颜色 - How to change color of dynamic row in collection-view swift 5 如何获取原型UICollectionViewCell的实例,以便可以使用可变大小的集合视图单元格? - How can I obtain an instance of a prototype UICollectionViewCell so that I can have variable sized collection view cells? 如何显示库中的实时照片视频并在iOS Swift的收藏夹视图中显示? - How to display Live-photos Videos from library and display in Collection-View in iOS Swift? 如何实现小视图变成全屏视图的效果? 类似于 Snapchat 从集合视图到内容视图的转换 - How to achieve effect where small view becomes full screen view? Similar to Snapchat transition from collection-view to content view 集合视图(创建可拖动的UICollectionViewCell)致命错误 - Collection View (Creating a Draggable UICollectionViewCell) Fatal Error UICollectionViewCell中的当前集合视图控制器 - present collection view controller from a UICollectionViewCell 从UICollectionViewCell类移除和重新加载集合视图 - Removing and Reloading collection view from UICollectionViewCell class 随机大小的UICollectionViewCell圆形设计 - Randomly sized UICollectionViewCell circle design 具有2个多行UILabels的UICollectionViewCell的高度/自定义大小的收集视图单元格 - Height of UICollectionViewCell with 2 multiline UILabels / self sizing collection view cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM