【发布时间】:2017-08-10 08:41:05
【问题描述】:
我构建了一个 iOS 应用程序,并将 Collection View 与 Collection View Cell 结合使用。我已经将 Collection View 约束设置为主视图。但是,但是,如果我运行该应用程序,它会给出不同的结果。甚至,在 iPhone 5 版本中,内容与屏幕宽度重叠。
请看下面的截图
集合视图的内容与屏幕大小重叠。请与下面的 iPhone 7 和 7plus 版本进行比较
这是 iPhone 7p 版本
我已经尝试为上、左、下、右设置特定约束,但它没有给出任何结果。这是我的视图控制器故事板
我搜索了整个网站,但我只找到了如何设置动态高度,而不是宽度。我只想设置动态宽度,因为内容高度是恒定的。谁能给我一些建议?
注意:这是我的收藏视图代码(可能有人想看)
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let screenSize: CGRect = UIScreen.main.bounds
return CGSize(width: collectionPromo.bounds.size.width, height: collectionPromo.collectionViewLayout.collectionViewContentSize.height);
}
我尝试设置该代码的动态宽度(我在互联网上找到的)
注意:这不是图像视图内容模式的问题,因为重叠的是整个内容(包括标签),而不仅仅是图像视图 这是我的收藏视图代码
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellPromo", for: indexPath)
var labelPromo = cell.viewWithTag(3) as! UILabel
labelPromo.text = list[indexPath.row].Period
var imagePromo = cell.viewWithTag(2) as! UIImageView
let promimg = try? UIImage(data: Data(contentsOf: URL(string: list[indexPath.row].ImageUrl)!))
imagePromo.image = promimg!
imagePromo.contentMode = UIViewContentMode.scaleAspectFit
//imagePromo.layer.masksToBounds = true
imagePromo.clipsToBounds = true
cell.layer.masksToBounds = false
cell.layer.borderColor = UIColor.white.cgColor
cell.layer.borderWidth = 5.0
//cell.layer.contentsScale = UIScreen.main.scale
cell.layer.shadowOpacity = 0.25
cell.layer.shadowRadius = 5.0
cell.layer.shadowOffset = CGSize.zero
cell.layer.shadowPath = UIBezierPath(rect: cell.bounds).cgPath
cell.layer.shouldRasterize = true
cell.layer.backgroundColor = UIColor.white.cgColor
return cell
}
【问题讨论】:
-
图像视图有什么限制?
-
看故事板(右侧部分)
-
我认为你为 iphone 7 固定了 collectionview 的宽度,所以它在 iphone 5 中溢出
-
@PhuDuy 如何使其动态化?我只是设置了上下左右约束
-
@christ2702 您尝试将视图更改为 iphone 5,我发现您的图像中存在与宽度相关的约束
标签: ios iphone swift uicollectionview