菜鸟教程小白 发表于 2022-12-13 02:22:30

ios - 调整 UICollectionViewCells 的大小以始终填充 UICollectionView


                                            <p><p>我有一个水平滚动的 UICollectionView,其 UICollectionViewCells 与 Collection View 本身一样高,三分之一宽(因此一次适合三个)。 Collection View 是分页的,并使用默认的流布局。</p>

<p>单元格来自 .xib 文件并使用自动布局来保持内容到位。</p>

<p>我想要做的是让 Collection View 的高度在两个预定义的高度之间切换,并让单元格的高度始终填满可用空间,即使在动画期间也是如此。</p>

<p>collection view 动画是通过像这样对其 superview 的顶部约束进行动画处理来完成的,并且似乎可以工作:</p>

<pre><code>UIView.animateWithDuration(0.5, animations: { () -&gt; Void in
    self.collectionViewTopConstraint.constant = (self.collectionViewTopConstraint.constant == 50 ? 100 : 50)
    self.view.layoutIfNeeded()
})
</code></pre>

<p>然而,这使单元格保持不变。</p>

<p>我尝试在动画 block 的末尾添加以下内容:</p>

<pre><code>self.collectionView.performBatchUpdates({ () -&gt; Void in

}, completion: nil)
</code></pre>

<p>这几乎可行,但单元格从一个高度交叉淡入到另一个高度,而不是仅仅改变高度,这看起来很糟糕。 </p>

<p>我还尝试了以下方法:</p>

<pre><code>self.collectionView.collectionViewLayout.invalidateLayout()
</code></pre>

<p>这样可以达到同样的效果,但是没有那么流畅。</p>

<p>另外,在这两种情况下,如果反复调用切换动画,我会收到以下警告:</p>

<pre><code>the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values.
</code></pre>

<p>我已经进行了大量研究,但我发现的所有内容都涉及自定义布局或在不同布局之间切换,我认为这不适用于我,因为即使 Collection View 的高度发生变化,布局也使用相同的逻辑来定位元素。</p>

<p>有什么想法吗?</p>

<p>谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为这可以通过在动画之前更改单元格的 itemSize 来实现。单元格的原始大小设置为如下所示的相同值,但没有 +sign*50 项。</p>

<pre><code>- (IBAction)toggleCollectionViewSize:(id)sender {
    self.collectionViewTopConstraint.constant = (self.collectionViewTopConstraint.constant == 50)? 100 : 50;
    NSInteger sign = (self.collectionViewTopConstraint.constant == 50)? 1 : -1;
    self.layout.itemSize = CGSizeMake(self.collectionView.frame.size.width/3.0 , self.collectionView.frame.size.height + sign*50);
    [UIView animateWithDuration:.5 animations:^{
      ;
    }];
}
</code></pre>

<p>我在一个在顶部、中间和底部都有 View 的单元格上进行了测试。它看起来还不错,但是底部 View 有轻微的移动,我无法解释。当过渡发生时,您仍然会看到单元格的轻微褪色(其 alpha 值降低)。如果 Collection View 背景颜色与单元格背景颜色相同,则可以最小化这种视觉效果。</p>

<p><strong>编辑后:</strong></p>

<p>如果你使用计时器而不是 animateWithDuration 效果会更好;我不会那样褪色。 </p>

<pre><code>-(void)viewWillLayoutSubviews {
    self.layout.itemSize = CGSizeMake(self.collectionView.bounds.size.width/3.0 , self.collectionView.bounds.size.height - 1); // the -1 suppresses a warning about the height that you get on every toggle. I still get one warning at the start.
}


- (IBAction)toggleCollectionViewSize:(id)sender {
    NSInteger sign = (self.topCon.constant == 50)? 1 : -1;
    ;
}


-(void)shrink:(NSTimer *) aTimer {
    NSInteger sign = ;
    self.topCon.constant += sign;
    if (self.topCon.constant == 100 || self.topCon.constant == 50) {
      ;
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 调整 UICollectionViewCells 的大小以始终填充 UICollectionView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/26142172/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/26142172/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 调整 UICollectionViewCells 的大小以始终填充 UICollectionView