2017-03-11 70 views
1

我有一個圖像視圖的集合視圖,當在iPhone 7上的模擬器中運行時,佈局就是我想要的 - 每行顯示3個圖像,兩個圖像之間沒有間距。但是,當我在手機(iPhone 6+)上運行它時,圖像之間會有間距。如何在集合視圖中顯示圖像視圖根據屏幕大小自行調整大小?

這些都是建立佈局的功能:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 
    return UIEdgeInsetsMake(0,0,0,0) 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 
    return 0 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
    return 0 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    let imgWidth = view.bounds.width/3.0 
    let imgHeight = imgWidth 

    return CGSize(width: imgWidth, height: imgHeight) 
} 

我在想,sizeForItem會時指出,寬度是屏幕的寬度除以三,高度等於重新縮放圖像對此。但我想我對此感到困惑,也許它只是調整單元格大小,而不是調整內部的圖像視圖。不過,我不確定在哪裏可以將圖像視圖大小設置爲隨屏幕大小動態縮放。

如何更改它以便集合視圖中的圖像單元相應地調整其大小,並且每行保留3行,無間距,而不管屏幕大小如何?

編輯:

enter image description here

編輯2:把cell.postImage.contentMode = UIViewContentMode.scaleAspectFillcellForItem方法後試圖iOSFreak的解決方案後,問題仍然存在:

enter image description here

+0

您是否在使用'myImageView.contentMode = UIViewContentModeScaleAspectFill'? – Zhang

+0

我將圖像視圖內容模式設置爲縮放以填充故事板。然而,當我嘗試了你的建議,並添加了'cell.postImage.contentMode = UIViewContentMode.scaleAspectFill'到我的'cellForItem'方法中時,它在一定程度上解決了問題,但並不完全 - 現在一些單元格並排正確對齊,他們的側面有空間。第一排和第二排之間仍有空間。我會嘗試上傳一張照片。 – KingTim

+0

我編輯了我的帖子以顯示我現在看到的內容。 – KingTim

回答

1

只要你有固定到使用自動佈局小區0頂,0底,0領先,和0尾隨或.flexible高度的自動尺寸調整和收集視圖細胞的ImageView的。靈活的寬度我不明白爲什麼它不會在你的例子中工作。你的方法看起來不錯。

在聲明示例中是否有UICollectionViewDelegateFlowLayout?

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {} 
+0

這最終成爲它 - 我沒有在集合視圖中的圖像視圖上設置自動佈局。一旦我補充說,我回到我的原始代碼,一切看起來不錯。 – KingTim

+0

太棒了。很高興它解決了它 – agibson007

0

請嘗試下面的代碼

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    let imgWidth = CGRectGetWidth(collectionView.frame)/3.0 
    return CGSize(width: imgWidth, height: imgWidth) 
} 

如果這不起作用,創建的UICollectionViewFlowLayout一個子類,如下圖所示

更新到3.0迅速

class CustomImageLayout: UICollectionViewFlowLayout { 

var numberOfColumns:CGFloat = 3.0 

override init() { 
    super.init() 
    setupLayout() 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    setupLayout() 
} 

override var itemSize: CGSize { 
    set { } 
    get { 
     let itemWidth = (self.collectionView!.frame.width - (self.numberOfColumns - 1))/self.numberOfColumns 
     return CGSize(width: itemWidth, height: itemWidth) 
    } 
} 

func setupLayout() { 
    minimumInteritemSpacing = 1 // Set to zero if you want 
    minimumLineSpacing = 1 
    scrollDirection = .vertical 
} 
} 

您需要設置集合視圖佈局,自定義佈局。

class ViewController: UIViewController { 

    @IBOutlet weak var colleciton: UICollectionView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    colleciton.collectionViewLayout = CustomImageLayout() 
    // Do any additional setup after loading the view, typically from a nib. 
} 
} 

Please find attached image of working code!

+0

第一種方法導致我發佈在我的編輯中的相同的東西,我不得不改變第二種方法(CGSizeMake已被棄用,所以我做了一個框架:'let frame = CGSize(width:itemWidth,height:itemWidth )'並且'返回CGSize(dictionaryRepresentation:frame as!CFDictionary)!'來擺脫錯誤。即使將'minimumInterlineSpacing'設置爲0,也導致所有單元格之間的間距。 – KingTim

+0

我剛剛嘗試過編輯版本爲Swift 3,圖像仍然沒有正確排列 - 行間有空格,某些圖像也有空間,我發佈了第二次編輯,並附有更新後的圖片。 – KingTim

+0

我會補充,我是不知道這是如何影響的東西,但我的第二次編輯中的四張圖片是從我手機的相機(貓,樹莓派和我的屏幕上的兩張照片)中取得的,其餘的則是從我的相機膠捲上傳的,只是s tock從互聯網上下載的照片。但是,故事板中的圖像視圖設置爲按比例填充,無論照片的來源如何,我不太確定爲什麼只有我的手機拍攝的照片在側面有間距,而互聯網圖片沒有。 – KingTim

相關問題