我構建了一個iOS應用程序,並將集合視圖與集合視圖單元一起使用。我已經將集合視圖約束設置爲主視圖。但是,但是,如果我運行該應用程序,它會給出不同的結果。即使在iPhone 5版本中,內容也與屏幕寬度重疊。如何設置動態寬度UICollectionViewCell
請看下面的截圖
集合視圖中重疊屏幕尺寸的內容。請在下面
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);
}
我試圖設置動態寬度代碼
NOTE(我在互聯網上找到):它不是圖像視圖內容模式的問題,因爲重疊一個人的全部內容(包括標籤),不僅圖像視圖 這是我收集的視圖代碼
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
}
什麼是圖像視圖的約束? –
看看故事板(右側部分) – christ2702
我認爲你固定寬度的collectionview iphone 7,所以它溢出在iPhone 5 –