0

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)
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
maxim rad
  • 67
  • 8
  • size of items func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: frame.width/4, height: frame.height) } – maxim rad Nov 17 '16 at 09:08
  • What is collectionTab? Is that the name of the UICollectionView? – Sajjon Nov 17 '16 at 09:43
  • Yes this is collection view – maxim rad Nov 17 '16 at 13:36

1 Answers1

1

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

override func viewWillLayoutSubviews {
    myCollectionView.collectionViewLayout.invalidateLayout()
}
Community
  • 1
  • 1
Sajjon
  • 8,938
  • 5
  • 60
  • 94