Tri*_*ith 21 xcode ios uistoryboard
试图创建响应/自适应当我发现摩擦UICollectionViewCells中UIStoryboard.
我看到的问题是你似乎无法设置Cell Size每个人,Size Class而我正在努力确定正确的方法.我设计了细胞以适应它们的容器,因此无论大小等级如何,它们都应该自动调整大小.这主要是因为如果我改变大小类,选择我的单元格视图Update Frames然后他们都调整大小以适应他们的新大小.然而,这是一次性的交易,如果我回到任何/任何规模的类,那么我仍然看到调整大小的版本.
这是我知道我可以尝试的:
UICollectionViews我希望这是一个解决方案,我只是遗漏了一些东西,但我知道可能是IB工具在这一点上与代码不匹配.
Tri*_*ith 15
我想出的解决方案就是实现UICollectionViewDelegateFlowLayout并实现该sizeForItemAtIndexPath方法.这意味着可以将单元格尺寸设置为与可用Size Class尺寸匹配.
这仍然不理想,因为您无法看到故事板中的更改,并且您无法创建通用设计并以每种不同的格式查看它.
我仍然希望有人有更好的选择.
这是一个在Swift中编码的类似解决方案.我只是在故事板中设置了我的两个单元格,并让它们可以查看任何大小类组合.当特征集合改变时,我更新了我想要使用的cellSize和cellReuseID,并告诉collectionView重新加载所有可见的单元格.然后
collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
Run Code Online (Sandbox Code Playgroud)
和
override func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
Run Code Online (Sandbox Code Playgroud)
(调用示例代码中未显示)可以让我更新单元格的大小并更新单元格的故事板样式.所以在故事板中并没有完全完成,但在Xcode中提供更多支持之前已经足够了.
struct MyCollectionViewConstants{
static let CELL_ANY_ANY_REUSE_ID = "cell";
static let CELL_COMPACT_REGULAR_REUSE_ID = "cellSmall"
static let CELL_ANY_ANY_SIZE = 100;
static let CELL_COMPACT_REGULAR_SIZE = 70;
}
class MyCollectionView: UICollectionViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var cellSize = MyCollectionViewConstants.CELL_ANY_ANY_SIZE
var cellReuseID = MyCollectionViewConstants.CELL_ANY_ANY_REUSE_ID
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
return CGSize(width: cellSize, height: cellSize)
}
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClass.Compact
&& self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClass.Regular){
cellSize = MyCollectionViewConstants.CELL_COMPACT_REGULAR_SIZE
cellReuseID = MyCollectionViewConstants.CELL_COMPACT_REGULAR_REUSE_ID
} else {
cellSize = MyCollectionViewConstants.CELL_ANY_ANY_SIZE
cellReuseID = MyCollectionViewConstants.CELL_ANY_ANY_REUSE_ID
}
self.collectionView.reloadItemsAtIndexPaths(
self.collectionView.indexPathsForVisibleItems())
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9220 次 |
| 最近记录: |