2016-05-10 18 views
1

Ich versuche eine Scroll-Sammlungsansicht mit 2 Bildspalten zu erstellen. Die Bilder am Ende werden unterschiedliche Höhe haben, aber die gleiche Breite, um nur die Hälfte des Bildschirms vertikal aufzunehmen, aber wie Sie sehen können, gibt es viel Platz für Schwarz.Sammlungsansicht tiling

Sample image

Hier ist der Code.

import UIKit 

Klasse Viewcontroller: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

let screenSize: CGRect = UIScreen.mainScreen().bounds 
@IBOutlet var collectionView: UICollectionView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { 
     let itemWidth = CGFloat(Int(view.bounds.width/3.0)) 
     let itemHeight = layout.itemSize.height 
     layout.itemSize = CGSize(width: itemWidth, height: itemHeight) 
     layout.invalidateLayout() 
    } 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as UICollectionViewCell! 
    return cell 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    collectionView.registerClass(ImageCell.self, forCellWithReuseIdentifier: "Cell") 
    return 10 
} 

} 

Und für die Zelle selbst:

import Foundation 
import UIKit 

class ImageCell: UICollectionViewCell{ 

var imageView: UIImageView! 


override init(frame: CGRect) { 
    super.init(frame: frame) 

} 

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

    imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)) 
    imageView.contentMode = UIViewContentMode.ScaleAspectFit 
    imageView.image = UIImage(named: "defaultImage.jpg") 
    contentView.addSubview(imageView) 

    //fatalError("init(coder:) has not been implemented") 
} 
} 
+0

Sie müssen schauen in T er 'minimumLineSpacing' und die' minimumInteritemSpacing' Attribute von 'UICollectionViewFlowLayout'. Sie können diese Eigenschaften in Ihrem Flow-Layout optimieren, um den Speicherplatz zu entfernen. Siehe die Dokumentation [hier] (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewFlowLayout_class/). Sie müssen auch sicherstellen, dass Ihre Artikelbreite den verfügbaren Platz auf dem Bildschirm ausnutzt. – keithbhunter

Antwort

0

verwenden Delegatfunktion

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

      CGSize screenSize=UIScreen.mainScreen().bounds.size 

     return CGSizeMake(screenSize.width/2, yourheighteOfImage); 
     } 

    func collectionView(_ collectionView: UICollectionView, 
             layoutcollectionViewLayout:UICollectionViewLayout,minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { 
     return 0.0 
    } 
+0

Gib auch hier die Bildhöhe zurück –

Verwandte Themen