2017-08-10 188 views
0

我構建了一個iOS應用程序,並將集合視圖與集合視圖單元一起使用。我已經將集合視圖約束設置爲主視圖。但是,但是,如果我運行該應用程序,它會給出不同的結果。即使在iPhone 5版本中,內容也與屏幕寬度重疊。如何設置動態寬度UICollectionViewCell

請看下面的截圖

iphone 5 version

集合視圖中重疊屏幕尺寸的內容。請在下面

iphone 7 version

iPhone的7和7Plus的版本進行比較,這是iPhone 7P版本

iphone 7p

我已經嘗試設置具體限制爲上,左,下,對,但它沒有結果。這是我的視圖控制器

storyboard

我在整個網站上搜索的故事板,但我只找到了如何設置動態的高度,而不是寬度。我只想設置動態寬度,因爲內容高度是不變的。任何人都可以給我任何建議嗎?

注:這是我收集的視圖代碼(也許有人想看看)

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 
} 
+0

什麼是圖像視圖的約束? –

+0

看看故事板(右側部分) – christ2702

+0

我認爲你固定寬度的collectionview iphone 7,所以它溢出在iPhone 5 –

回答

0

enter image description here您可以將背景顏色添加到圖像視圖。並看到它在方面適合。您需要在應用中添加不處於肖像模式的圖像。你也需要添加約束,所以沒有圖像擴展邊界。

簡單的建議是調試它在特定設備上傳播的原因嗎? 按照三個步驟 1)使用背景顏色

2)約束上的ImageView

3)與圖像寬高比

Try this - let IS_OS_8_OR_LATER = 
    Float(UIDevice.currentDevice().systemVersion) >= 8.0 

    let IS_IPHONE = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone 

    let IS_STANDARD_IPHONE_6 = (IS_IPHONE && 
    UIScreen.mainScreen().bounds.size.height == 667.0 && IS_OS_8_OR_LATER && 
    UIScreen.mainScreen().nativeScale == UIScreen.mainScreen().scale) 

let IS_ZOOMED_IPHONE_6 = (IS_IPHONE && 
UIScreen.mainScreen().bounds.size.height == 568.0 && IS_OS_8_OR_LATER && 
UIScreen.mainScreen().nativeScale > 
UIScreen.mainScreen().scale) 

let IS_STANDARD_IPHONE_6_PLUS = (IS_IPHONE && 
UIScreen.mainScreen().bounds.size.height == 736.0) 

let IS_ZOOMED_IPHONE_6_PLUS = (IS_IPHONE && 
UIScreen.mainScreen().bounds.size.height == 667.0 && IS_OS_8_OR_LATER && 
UIScreen.mainScreen().nativeScale < UIScreen.mainScreen().scale) 

if IS_ZOOMED_IPHONE_6_PLUS { 
    //do something 
} 
+0

約束圖像視圖已經是0,0,0,0 – christ2702

+0

iPhone 7plus版本,集合視圖單元的左側和右側是額外的空間。 – christ2702

+0

它具有圖像方面問題的簡單問題佈局。只是嘗試添加更多的約束,並玩填充和適合的方面。 – cole

0

使用的CollectionView方法來設置的CollectionView邊緣播放。 Collection View自動計算其動態寬度。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets 
{ 
    return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) 
} 

或者U可以使用這個。

但是儘量把下面的代碼裏面viewDidLoad中():

collectionView!.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) 

這將添加填充到的CollectionView的每一個側面。

+0

代碼無法正常工作 – christ2702