0

Je rencontre des problèmes de disposition avec collectionViewCells. J'applique par programme des contraintes de disposition et UICollectionViewCells ne sont pas épinglés à la position 0,0 dans mon collectionView. Voir la capture d'écran ci-jointe pour référence. Merci d'avance!collectionViewCells to collectionAfficher la position x: 0, y: 0

class CurrentUserPlaceDetailsVC: UIViewController, UICollectionViewDelegateFlowLayout { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     setupMenuBar() 
    } 

    let menuBar: MenuBar = { 
     let mb = MenuBar() 
     return mb 
    }() 

    private func setupMenuBar() { 
     view.addSubview(menuBar) 
     view.addConstraintsWithFormat("H:|[v0]|", views: menuBar) 
     view.addConstraintsWithFormat("V:|-64-[v0(150)]", views: menuBar) 

    } 
    var placesTableVC: PlacesTableVC? 
} 

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

    lazy var collectionView: UICollectionView = { 
     let layout = UICollectionViewFlowLayout() 
     let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     cv.backgroundColor = UIColor.green 
     cv.dataSource = self 
     cv.delegate = self 
     return cv 
    }() 

    let cellId = "cellId" 

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


     collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId) 

     addSubview(collectionView) 
     addConstraintsWithFormat("H:|[v0]|", views: collectionView) 
     addConstraintsWithFormat("V:|[v0]|", views: collectionView) 

    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 4 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) 
     cell.backgroundColor = .blue 
     return cell 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

} 


extension UIView { 

    func addConstraintsWithFormat(_ format: String, views: UIView...) { 
     var viewsDictionary = [String: UIView]() 
     for (index, view) in views.enumerated() { 
      let key = "v\(index)" 
      view.translatesAutoresizingMaskIntoConstraints = false 
      viewsDictionary[key] = view 
     } 

     addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary)) 
    } 

enter image description here

+0

possible en double http://stackoverflow.com/questions/43023384/uicollectionview-remove-top-padding/43025517#43025517 – Joe

Répondre

0

Vous devez définir le minimumLineSpacing, headerReferenceSize et/ou sectionInset de l'instance UICollectionViewFlowLayout que vous utilisez pour initialiser votre UICollectionView. Vous pouvez également définir le contentInset du UICollectionView.

Essayez ceci:

lazy var collectionView: UICollectionView = { 
    let layout = UICollectionViewFlowLayout() 
    layout.minimumLineSpacing = 0 
    layout.headerReferenceSize = .zero 
    layout.sectionInset = .zero 
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
    cv.backgroundColor = .green 
    cv.dataSource = self 
    cv.delegate = self 
    cv.contentInset = .zero 
    return cv 
}()