菜鸟教程小白 发表于 2022-12-11 20:36:48

ios - 无法使用 AutoLayout 设置 UICollectionViewCell 的宽度


                                            <p><p>我有一个 Collection View ,其中包含动态高度的单元格(基于可能的多行标签和内部的 TextView ),当我将它跨越多行时,它可以完美地适应高度。但是,当文本只有一个单词或没有覆盖整个屏幕宽度时,单元格的宽度与需要的一样宽,从而导致单元格彼此相邻而不是彼此下方。</p>

<p> <a href="/image/mD2sn.jpg" rel="noreferrer noopener nofollow">Take a look at the result with different lengths of text inside the textview.</a> </p>

<p>但是,我希望单元格彼此位于下方,想想 Instagram 或 Twitter 那种 View 。我显然需要为单元格设置宽度约束,使其等于屏幕的整个宽度,但我似乎找不到如何做到这一点。</p>

<p>我在 AppDelegate 中使用 FlowLayout 和估计的ItemSize:</p>

<pre><code>let layout = UICollectionViewFlowLayout()
window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))

layout.estimatedItemSize = CGSize(width: 414, height: 150)
</code></pre>

<p>我还在viewDidLoad中为collectionView设置了约束,所以collectionView占据了屏幕的整个宽度和高度:</p>

<pre><code>collectionView?.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
collectionView?.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
collectionView?.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
collectionView?.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
</code></pre>

<p>我对标签和 TextView 的约束看起来像这样,也允许宽度采用所需的大小(所以它不是固定的):</p>

<pre><code>// vertical constraints usernameLabel &amp; commentTextView
addConstraintsWithFormat(format: &#34;V:|-16--2--16-|&#34;, views: usernameLabel, commentTextView)

// horizontal constraints usernameLabel
addConstraintsWithFormat(format: &#34;H:|-16--16-|&#34;, views: usernameLabel)

// horizontal constraints commentTextView
addConstraintsWithFormat(format: &#34;H:|-16--16-|&#34;, views: commentTextView)
</code></pre>

<p>在我看来,所有这些对于完成这项工作至关重要。但是,在我看来,我还没有设置宽度约束来完成这项工作。我试图在 collectionView 上添加一个 widthAnchor(等于 view.widthAnchor),这不起作用:</p>

<pre><code>collectionView?.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
</code></pre>

<p>我也尝试在 sizeForItemAt 方法中设置宽度,但这也没有用:</p>

<pre><code>return CGSize(width: view.frame.width, height: 100)
</code></pre>

<p>现在,我正在检查宽度,看看哪里出了问题,但我得到了我需要的结果,我想:</p>

<pre><code>print(&#34;collectionView width: &#34;, collectionView?.frame.size.width)
print(&#34;collectionView height: &#34;, collectionView?.frame.size.height)

print(&#34;screen width: &#34;, UIScreen.main.bounds.width)
print(&#34;screen height: &#34;, UIScreen.main.bounds.height)
</code></pre>

<p>结果:</p>

<pre><code>collectionView width:Optional(414.0)
collectionView height:Optional(736.0)
screen width:414.0
screen height:736.0
</code></pre>

<p>我不知道如何前进。我浏览过的与此问题相关的所有文章或帖子要么明确计算高度,要么使用我上面提到的两种解决方案。</p>

<p>关于我做错了什么有什么想法吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>更新:</strong></p>

<p>我根据 Siju Satheesachandran 的回答为我的 HomeController 添加了一个扩展:</p>

<pre><code>extension HomeController {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
      return CGSize(width: 400, height: 100)
    }
}
</code></pre>

<p>然后,在 cellForItemAt 方法中,我添加了当前单元格宽度的打印:</p>

<pre><code>print(&#34;cell width: &#34;, cell.frame.size.width)
</code></pre>

<p>返回以下五次:</p>

<pre><code>cell width:400.0
</code></pre>

<p>所以在我看来,需要的尺寸是被聆听的,但我的单元格只是适应它想要的任何宽度?</p>

<p><strong>另一个有趣的更新:</strong></p>

<p>当我将 TextView 的文本更改为太长时,它应该跨越多行,并且我运行应用程序时,应用程序在不加载和显示黑屏的情况下无限次给出此调试错误:</p>

<pre><code>2018-09-18 15:34:35.806688+0200 prjDynamicCells The behavior of the UICollectionViewFlowLayout is not defined because:
2018-09-18 15:34:35.807472+0200 prjDynamicCells the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
2018-09-18 15:34:35.807572+0200 prjDynamicCells Please check the values returned by the delegate.
2018-09-18 15:34:35.807911+0200 prjDynamicCells The relevant UICollectionViewFlowLayout instance is &lt;UICollectionViewFlowLayout: 0x7feb5ec17650&gt;, and it is attached to &lt;UICollectionView: 0x7feb5f841600; frame = (0 0; 414 736); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = &lt;NSArray: 0x60000044e6d0&gt;; layer = &lt;CALayer: 0x60000003f4a0&gt;; contentOffset: {0, -64}; contentSize: {414, 1000}; adjustedContentInset: {64, 0, 0, 0}&gt; collection view layout: &lt;UICollectionViewFlowLayout: 0x7feb5ec17650&gt;.
2018-09-18 15:34:35.808009+0200 prjDynamicCells Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法使用 AutoLayout 设置 UICollectionViewCell 的宽度,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/52383858/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/52383858/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法使用 AutoLayout 设置 UICollectionViewCell 的宽度