我正在尝试UICollectionViewCells使用自动布局进行自我调整,但我似乎无法让单元格根据内容调整自己的大小.我无法理解如何根据单元格内容中的内容更新单元格的大小.
这是我尝试过的设置:
UICollectionViewCell一个UITextView.UITextView已禁用.UITextView固定在单元格左侧,显式宽度为320.UITextView固定到单元格的顶部.UITextView具有高度限制设置为其中一个恒定的UITextView报告将适合文本.这是单元格背景设置为红色,UITextView背景设置为蓝色的样子:

我把我已经在GitHub上玩的项目在这里.
我做了一些研究,但是我找不到任何关于如何在UICollectionView中水平居中单元格的代码示例.
而不是第一个单元格像这个X00,我希望它像这个0X0.有没有办法实现这个目标?
编辑:
想象我想要的东西:
当CollectionView中只有一个元素时,我需要它看起来像版本B. 当我有多个元素时,它应该像版本A但有更多元素.
当我只有1个元素时,它看起来像版本A,我想知道我怎么能让它看起来像B.
谢谢您的帮助!
我在我的项目中使用UICollectionView,其中一行中有多个不同宽度的单元格.根据:https: //developer.apple.com/library/content/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html
它使用相同的填充将细胞扩散到整个线上.这种情况按预期发生,除了我想左对齐它们,并硬编码填充宽度.
我想我需要继承UICollectionViewFlowLayout,但是在线阅读了一些教程等之后,我似乎并不知道这是如何工作的.
这是导致警告的代码:
private override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
let attributes = super.layoutAttributesForItemAtIndexPath(indexPath)
let distance = CGRectGetMidX(attributes!.frame) - self.midX;
var transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, -distance, 0, -self.width);
attributes!.transform3D = CATransform3DIdentity;
return attributes
}
Run Code Online (Sandbox Code Playgroud)
控制台还打印:
这可能是因为流布局"xyz"正在修改UICollectionViewFlowLayout返回的属性而不复制它们.
我该如何修复此警告?
如何UICollectionView使用自定义单元格生成椭圆形单元格.下面是我想要实现的图像.
不知道如何实现这一点,通过几个链接,但没有得到.请指导.谢谢
更新:我尝试的是
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width/3.0 - 8, height: collectionView.frame.size.width/2.5[![enter image description here][2]][2] )
}
Run Code Online (Sandbox Code Playgroud)
答案Objective-C和Swift2.0:如何居中对齐UICollectionView的单元格?
我通常会尝试将Swift2.0答案转换为Swift3.0解决方案,但方法是:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
let edgeInsets = (screenWight - (CGFloat(elements.count) * 50) - (CGFloat(elements.count) * 10)) / 2
return UIEdgeInsetsMake(0, edgeInsets, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)
似乎不存在Swift3.0,我找到的唯一有用的方法似乎是:
func collectionView(_ collectionView: UICollectionView, transitionLayoutForOldLayout fromLayout: UICollectionViewLayout, newLayout toLayout: UICollectionViewLayout) -> UICollectionViewTransitionLayout {
<#code#>
}
Run Code Online (Sandbox Code Playgroud)
但我不确定如何正确实施它.
如何对准一个UICollectionViewin 的细胞Swift3.0?
(iOS和tvOS的简单通用解决方案将是完美的)
我使用新的组合内容设置了一个集合视图及其布局,它非常酷,它似乎非常可扩展,但我找不到一种方法使项目集中在集合中,这是我认为的一个基本功能人们会想象得到支持..
我所拥有的是:
使用此代码完成:
UICollectionViewCompositionalLayout { section, env in
let tagDefaultSize = CGSize(width: 100, height: 40)
let item = NSCollectionLayoutItem(
layoutSize: NSCollectionLayoutSize(widthDimension: .estimated(tagDefaultSize.width), heightDimension: .absolute(tagDefaultSize.height))
)
item.edgeSpacing = NSCollectionLayoutEdgeSpacing(
leading: .fixed(0),
top: .fixed(0),
trailing: .fixed(8),
bottom: .fixed(0)
)
let group = NSCollectionLayoutGroup.horizontal(
layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(tagDefaultSize.height)),
subitems: [item]
)
group.edgeSpacing = NSCollectionLayoutEdgeSpacing(
leading: .flexible(8),
top: .fixed(0),
trailing: .fixed(8),
bottom: .fixed(8)
)
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(
top: 30,
leading: 20,
bottom: 40,
trailing: 20
)
return section …Run Code Online (Sandbox Code Playgroud) uikit ios uicollectionview swift uicollectionviewcompositionallayout
我UICollectionView只用了一个部分。这里是一些可视化:
[ x x x ]
[ x x x ]
[ x x ]
Run Code Online (Sandbox Code Playgroud)
如果的最后一行UICollectionView没有填满所有3个单元格,我想像这样将这些单元格居中:
[ x x x ]
[ x x x ]
[ x x ]
Run Code Online (Sandbox Code Playgroud)
我尝试从以下位置实施解决方案:
但是,解决方案会移动所有单元格,并且如果最后一行不包含X个单元格,则我只想移动LAST行,因为我只有一个部分。
我知道如何设置插图,但是我不知道如何仅靠一个部分并尝试调整单元格的最后一行的插图来实现这一点。
我刚刚开始使用UICollectionView,不确定如何解决此问题?
ios ×9
swift ×5
swift3 ×2
autolayout ×1
iphone ×1
objective-c ×1
tvos ×1
uicollectionviewcompositionallayout ×1
uikit ×1
uiscrollview ×1