2016-03-29 13 views
1

NSCollectionViewFlowLayout, sağ kenar boşluğunda gerekçelerle veya kapsayıcı yalnızca bir öğe için yeterince genişse öğelerle birlikte bir düzen oluşturur. Bir hizalama seçeneği bekliyordum, ör. delege üzerinde, ancak dokümanlardaki hiçbir şeyi bulamıyorum. Bunu başarmak için NSCollectionViewFlowLayout alt sınıfını gerektiriyor mu?NSCollectionViewFlowLayout - left alignment

class LeftFlowLayout: NSCollectionViewFlowLayout { 

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [NSCollectionViewLayoutAttributes] { 

     let defaultAttributes = super.layoutAttributesForElementsInRect(rect) 

     if defaultAttributes.isEmpty { 
      // we rely on 0th element being present, 
      // bail if missing (when there's no work to do anyway) 
      return defaultAttributes 
     } 

     var leftAlignedAttributes = [NSCollectionViewLayoutAttributes]() 

     var xCursor = self.sectionInset.left // left margin 

     // if/when there is a new row, we want to start at left margin 
     // the default FlowLayout will sometimes centre items, 
     // i.e. new rows do not always start at the left edge 

     var lastYPosition = defaultAttributes[0].frame.origin.y 

     for attributes in defaultAttributes { 
      if attributes.frame.origin.y != lastYPosition { 
       // we have changed line 
       xCursor = self.sectionInset.left 
       lastYPosition = attributes.frame.origin.y 
      } 

      attributes.frame.origin.x = xCursor 
      // by using the minimumInterimitemSpacing we no we'll never go 
      // beyond the right margin, so no further checks are required 
      xCursor += attributes.frame.size.width + minimumInteritemSpacing 

      leftAlignedAttributes.append(attributes) 
     } 
     return leftAlignedAttributes 
    } 
} 

cevap

8

bir sol haklı akış düzenini üreten bir alt sınıfıdır! `Attributes.frame.origin.y eğer = lastYPosition` olmalıdır` eğer attributes.frame.origin .y> lastYPosition ', yeni konumun son konumdan daha az olduğu bir senaryoyu engellemek için. Bu, UI öğelerinin birbiriyle çakışan çizilmesine neden olur. (düzenlemem reddedildi - bu düzeltmeyi bir yorum olarak yayınla)
+1

bir hata burada vardır: İşte – gh123man