【发布时间】:2016-10-18 05:54:25
【问题描述】:
我已将AutoLayout 与我的UICollectionView 之一一起使用,我正面临imageView 的高度和宽度问题。我在UICollectionViewCell 中添加了UIImageView,如下约束。
UICollectionViewDataSource 方法。
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var width = Int((collectionView.frame.size.width - 30) / 4)
if width % 2 != 0 {
width += 1
}
return CGSize(width: CGFloat(width), height: CGFloat(width))
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
print("Cell Size - \(cell.bounds)")
print("ImageView Size - \(cell.imageView.frame)")
return cell
}
但是当我运行它时,ImageView 总是与 70X70 一起出现,无论我将在哪个设备上运行,但 UICollectionViewCell 的大小在不同的设备上会增加和减少。
在 iPhone 6 上如下所示。
我已将UIImageView 的backgroundColor 设置为UICollectionViewCell 的白色和红色,以便击球手了解Cell 和ImageView Size 的控制台日志如下。
Cell Size - (0.0, 0.0, 86.0, 86.0)
ImageView Size - (5.0, 5.0, 70.0, 70.0)
Cell Size - (0.0, 0.0, 86.0, 86.0)
ImageView Size - (5.0, 5.0, 70.0, 70.0)
请任何人帮助我为什么 imageView 的大小没有根据UICollectionViewCell 增加或减少。任何帮助将不胜感激。
【问题讨论】:
-
为什么 x 和 y ...我认为没有必要?
-
@EICaptainv2.0 我已经尝试过不使用它,但它不会工作。
-
我看到这个尺寸等级在你的故事板中被选中。如果你选择全部,你还能看到约束吗?
-
@CarienvanZyl 不明白你想说什么。
-
在约束部分,在约束的可视化表示下方,有一个段控件,位于 All 和 This Size Class 之间。如果您仅为此尺寸类定义约束,它们也不会应用于其他尺寸类。如果您打开约束,则不会检查已安装,而是检查一个大小等级,例如。可以检查 wR hC。见developer.apple.com/library/content/documentation/…。
标签: ios uiimageview autolayout uicollectionview uicollectionviewcell