skip to Main Content

I have collectionView like this

enter image description here

I’m calling different 3 xib files to cells like this

collectionView.register(UINib(nibName: "OfficialVehicleHeader", bundle: nil), forCellWithReuseIdentifier: "OfficialVehicleHeaderCell")
collectionView.register(UINib(nibName: "OfficialVehicleMedium", bundle: nil), forCellWithReuseIdentifier: "OfficialVehicleMediumCell")
collectionView.register(UINib(nibName: "OfficialVehicleBottom", bundle: nil), forCellWithReuseIdentifier: "OfficialVehicleBottomCell")

My collectionView layout code

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
collectionView.setCollectionViewLayout(layout, animated: true)
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize

In the xib file i set constraint (leading, trailing, bottom, top as 0) in addition height constraint. The height is running awesome no problem but i don’t want to set width constraint. Because there is different between screens

Where did i mistake ?

2

Answers


  1. Chosen as BEST ANSWER

    I solved the problem. I need to set width constraint but i don't want to set. I solve this way

    1. Go to xib file and set width constraint

    2. Go to size inspector and select that you created width constraint

    3. Set identifier for example "myConstraint"

    4. Open your cell file and define IBOutlet

      for constraint in self.containerView.constraints {
        if constraint.identifier == "myConstraint" {
          constraint.constant = UIScreen.main.bounds.size.width * 0.70
        }
      }
      containerView.layoutIfNeeded()
      

    I added image that i mentioned

    enter image description here

    enter image description here


  2. You have to set leading and trailing it will set width according to devices width. But don’t forget to make your collection views constraints to be resizable.

    Here is my sample code on how to do it:

    collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        collectionView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        collectionView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 140).isActive = true
        collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
    

    layout settings:

    layout.itemSize = CGSize(width: self.view.frame.width - 10, height: self.view.frame.height / 3)
    

    Cells constraints to be resizable:

    cell.widthAnchor.constraint(equalTo: widthAnchor, constant: 0).isActive = true
            cell.heightAnchor.constraint(equalTo: heightAnchor, constant: 0).isActive = true
            cell.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true
            cell.rightAnchor.constraint(equalTo: rightAnchor, constant: 0).isActive = true
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search