Omar HossamEldin Omar HossamEldin - 1 month ago 12
Swift Question

UIImageView overlaps UILabel on next ViewController Open

I have a parent ViewController which opens a second one.
Second ViewController contains a grid view like in the image.

Correct Image

When I go back and enter same screen again I found layout changed to this

Wrong Image

Here in the second image, the UIImage overlaps the label.

Here is my InterfaceBuilder Settings

enter image description here

I change the size and EdgeInsets using following code

func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAtIndexPath: IndexPath) -> CGSize {
let width = collectionView.frame.size.width/2 - 8*2
if let cell = collectionView.cellForItem(at: sizeForItemAtIndexPath) {
return CGSize(width: width, height: cell.frame.height)
} else {
return CGSize(width: width, height: 104)

func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, insetForSectionAtIndex : NSInteger) -> UIEdgeInsets {
return UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8);

Why do the first time only the layout is correct and after that the layout changes, Any Clues?


Here is a screen shot for 3d Debugging

3d debugging first open

3d debugging second launch


Try to give aspect ratio constraint from UILabel to image. Hit and then run. But before this make sure you did not want a static height for you imageView. If you want to the height of imageView to not be increase or decrease device wise then instead of aspect ratio just give a static height constraint to imageView. Your problem will be solve.