-1

I have a UILabel in my storyboard which i want to align from bottom and whose width and height want to get adjusted in portrait and landscape.

For this, i dragged in a UILabel pinned its leading space, trailing space, bottom space to its superview.

I did not pin its width and height since i wanted its width and height to be changed during its landscape rotation.

With this i got a warning in story board where i have selected to update frame, which automatically adjusted the frame and made it work correctly.

Now i am trying to achieve same kind of behaviour with UICollectionView but this is not working as expected, instead it is giving out some errors saying 'misplaced view' and 'missing constraints'.

I am aligning buttons in 2 rows and 3 columns in the collection view in portrait mode and want to align all the 6 in single row in landscape mode, so if i pin the height here.

The problem is it will go good for portrait mode but in landscape the height will seem higher. Any help on how this could be achieved.?

Need to have something like this for UICollectionView in portrait and like this in landscape Need to achieve similar behaviour for this in landscape.

Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
XiOS
  • 1,665
  • 2
  • 18
  • 22

1 Answers1

1

You got errors because you did not added the constraint for the collection view top layout or height or aspect ratio. it is necessary to assign all side constraint so the view will automatically layout according to the constraint. if you miss one it will give you error or warning. Look you want dynamic height for collection view. If you want to achieve this for all the devices like for all iphones then you need to pin the height and make a outlet of the height constraint and change it in your viewDidLayoutSubviews method. select the collection view then go to size inspector and double click the height constraint. this will show you the constraint on the document outline. make outlet of that and change that constraint according to your need like on portrait or landscape you can calculate with logic. you might need to change the constraint according to the orientation change. follow https://stackoverflow.com/a/24072010/4030971

i know that will be great if all those stuff work only with some constraint but i found this way easier for me.

Community
  • 1
  • 1
Mahesh Agrawal
  • 3,348
  • 20
  • 34