0

Ich brauche 2 Zellen für jede Zeile.UICollectionView falsche Breite in Zellen

Mit genau der Mitte der Controller-Breite.

In iPhone 5 Fall. Die Bildschirmbreite ist 320 und jede Zeile wird 160 sein.

self.view.frame.size.width => 320.0 
midleWidth => 160.0 

Aber wenn die Collection die Zelle getrennt sind drawed ist, wie das nächste Bild:

enter image description here

Meine Collection config:

enter image description here

Wenn i Setzen Sie die self.view.frame.size.width als die Breite der Zelle, Nehmen Sie die gesamte Breite des Bildschirms wie erwartet.

+0

haben Zellenabstand im Code gesetzt, wo? –

Antwort

2

Versuchen Sie, diese Below-Code

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(
     "Cell", forIndexPath: indexPath) 
    let viewsDictionary = ["View1": collectionView] 
    collectionView.translatesAutoresizingMaskIntoConstraints = false 
    self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[View1]|", options: [], metrics: nil, views: viewsDictionary)) 
    self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[View1]|", options: [], metrics: nil, views: viewsDictionary)) 

    return cell 
} 

func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize 
{ 
    let cellSize:CGSize = CGSizeMake(self.bounds.width/2, self.bounds.height) 
    return cellSize 
} 
+0

Danke für aswer, Arbeitet, aber gibt eine große Warnung für jede Zelle zurück, die mit Layoutproblemen verbunden ist. 'Constraint kann nicht gleichzeitig erfüllt werden und' 'und' ' – jose920405

+0

versuchen, diesen Code hinzuzufügen - cell.contentView.frame = cell.bounds cell.contentView.autoresizingMask = [.FlexibleBreite, .FlexibleHeight] – ignotusverum

0

// wie diese verwenden es für alle Geräte funktionieren

- (CGSize)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout *)collectionViewLayout 
    sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    CGSize size; 
    CGRect bounds = [[UIScreen mainScreen] bounds]; 
    double bWidth = (bounds.size.width/320)* 200 ; // 200 is your cell width which given in xib 
    double bheight = (bWidth/320)* 250 // 250 is your cell height which given in xib; 
    size = CGSizeMake(bWidth, bheight); 

    return size; 
} 
+0

diese Zeile '(bounds.size.width/320) * 200' immer in iphone 5 ist 200. ->' (320/320) * 200 = 200'. Und 200 ist nicht die Mitte des Bildschirms. Wenn "200" durch "bounds.size.width/2" geändert wird, erscheint wieder der gleiche Abstand zwischen den Zellen – jose920405

+0

Sie müssen dort Ihre Zellenbreite angeben, ich gebe die Beispielwerte –

0

Die obige Antwort wird nicht funktionieren für alle Geräte versuchen, diese

- (CGSize)collectionView:(UICollectionView *)collectionView 
          layout:(UICollectionViewLayout*)collectionViewLayout 
      sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 

      CGSize windowSize=[[[UIApplication sharedApplication] delegate] window].frame.size; 

      return CGSizeMake(windowSize.width/2,heightOfCell); 
     } 
    - (CGFloat)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout *)collectionViewLayout 
minimumLineSpacingForSectionAtIndex:(NSInteger)section { 
return 0.0 
} 

    - (CGFloat)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout *)collectionViewLayout 
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { 
    return 0.0 
    } 
+0

Danke für die Antwort, aber das gleiche Ergebnis. Derselbe Raum. – jose920405

+0

@ jose920405 Versuchen Sie die bearbeitete Antwort –

+0

Danke Mann, aber nicht funktioniert. Diese 2 Funktionen sind die gleichen, die ich in meiner Frage in 'Meine CollectionView config is:' hinzugefügt habe. Versuchen Sie auch, aber nichts – jose920405

Verwandte Themen