maxim rad maxim rad - 23 days ago 5
iOS Question

Why are the constraints not working?

Portrait mode

enter image description here

Landscape mode

enter image description here

Tab Bar implemented using

UICollectionView
(CV). In the CV I have added 4 items:

addSubview(collectionTab)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":collectionTab]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":collectionTab]))


When I change the orientation the constraints are not working. Why? How can I fix it?
I think the size of items is not recalculated when I change the view.

Size of items:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: frame.width/4, height: frame.height)
}

Answer

Try running invalidateLayout on the UICollectionViewLayout on your UICollectionView. Like this

override func viewWillLayoutSubviews {
    myCollectionView.collectionViewLayout.invalidateLayout()
}