• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:36:48 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

Take a look at the result with different lengths of text inside the textview.

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

我在 AppDelegate 中使用 FlowLayout 和估计的ItemSize:

let layout = UICollectionViewFlowLayout()
window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))

layout.estimatedItemSize = CGSize(width: 414, height: 150)

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

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

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

// vertical constraints usernameLabel & commentTextView
addConstraintsWithFormat(format: "V:|-16-[v0]-2-[v1]-16-|", views: usernameLabel, commentTextView)

// horizontal constraints usernameLabel
addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: usernameLabel)

// horizontal constraints commentTextView
addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: commentTextView)

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

collectionView?.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true

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

return CGSize(width: view.frame.width, height: 100)

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

print("collectionView width: ", collectionView?.frame.size.width)
print("collectionView height: ", collectionView?.frame.size.height)

print("screen width: ", UIScreen.main.bounds.width)
print("screen height: ", UIScreen.main.bounds.height)

结果:

collectionView width:  Optional(414.0)
collectionView height:  Optional(736.0)
screen width:  414.0
screen height:  736.0

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

关于我做错了什么有什么想法吗?



Best Answer-推荐答案


更新:

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

extension HomeController {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 400, height: 100)
    }
}

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

print("cell width: ", cell.frame.size.width)

返回以下五次:

cell width:  400.0

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

另一个有趣的更新:

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

2018-09-18 15:34:35.806688+0200 prjDynamicCells[39793:1627318] The behavior of the UICollectionViewFlowLayout is not defined because:
2018-09-18 15:34:35.807472+0200 prjDynamicCells[39793:1627318] 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[39793:1627318] Please check the values returned by the delegate.
2018-09-18 15:34:35.807911+0200 prjDynamicCells[39793:1627318] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7feb5ec17650>, and it is attached to <UICollectionView: 0x7feb5f841600; frame = (0 0; 414 736); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x60000044e6d0>; layer = <CALayer: 0x60000003f4a0>; contentOffset: {0, -64}; contentSize: {414, 1000}; adjustedContentInset: {64, 0, 0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x7feb5ec17650>.
2018-09-18 15:34:35.808009+0200 prjDynamicCells[39793:1627318] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

关于ios - 无法使用 AutoLayout 设置 UICollectionViewCell 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52383858/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap