maxim rad maxim rad - 15 days ago 5
Swift Question

Why not working constrai

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 constraints 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()
}
Comments