UICollectionView, NSLayoutConstraint

185 Views Asked by At

I have the following codes:

import UIKit

class FeedController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

let CellId = "custom cell identifier"


override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = UIColor.white
    collectionView?.register(FeedCell.self, forCellWithReuseIdentifier: CellId)
    collectionView?.alwaysBounceVertical = true // patog amikor lehuzzuk, opcionalis

    navigationItem.title = "Rengeteng"
    // Do any additional setup after loading the view, typically from a nib.
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let customCell = collectionView.dequeueReusableCell(withReuseIdentifier: CellId, for: indexPath as IndexPath)
    as! FeedCell
    customCell.nameLabel.text = names[indexPath.item]
    return customCell
}
let names = ["asd1", "asd2", "asd3"]

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return names.count
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let screenSize: CGRect = UIScreen.main.bounds
    let view = UIView(frame: .zero)
    view.frame.size = CGSize(width: screenSize.width, height: 44)
    return view.frame.size
}



class FeedCell: UICollectionViewCell{

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

let nameLabel: UILabel = {
    let label = UILabel()
    label.text = "Custom Text"
    //label.font = UIFont.boldSystemFont(ofSize: 14) // betumeret

    return label
}()

let profileImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFit
    imageView.backgroundColor = UIColor.blue //vagy amilyet szeretnenk

    return imageView
}()


func setupViews(){
    backgroundColor = UIColor.red

    addSubview(nameLabel)
    addSubview(profileImageView)

    addConstraintsWithFormat(format:"H:|-8-[v0(==44)]-8-[v1]|", views: profileImageView, nameLabel)
    addConstraintsWithFormat(format:"V:|[v0]|", views: nameLabel)
    addConstraintsWithFormat(format:"V:|-8-[v0(==44)]|", views: profileImageView)

    /*
    addConstraint(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[v0(==44)]-8-[v1]|", options: NSLayoutFormatOptions, metrics: nil,
            views:["v0": profileImageView, "v1": nameLabel]))
    addConstraint(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[v0(==44)]-8-[v1]|", options: NSLayoutFormatOptions, metrics: nil,
                                                 views:["v0": profileImageView, "v1": nameLabel]))
    */
}

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


}

}


//ROHADT FONTOS CUCC
extension UIView{

func addConstraintsWithFormat(format: String, views: UIView...){

    var viewsDictionary = [String: UIView]()

    for(index,views) in views.enumerated(){

        let key = "v\(index)"
        viewsDictionary[key] = views
        //viewsDictionary[key]!.translatesAutoresizingMaskIntoConstraints = false
        views.translatesAutoresizingMaskIntoConstraints = false
    }
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
}
}

And I am getting the following error:

2016-12-26 11:16:44.196885 romosprogos[1142:27128] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "", "", "", "" )

I almost tried everything that I know but I am new at Xcode. Please help me!! :))

0

There are 0 best solutions below