Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCode: How to change layout of views between landscape and portrait mode

Tags:

In portrait mode I have four views on top of one another from the top to the bottom of the view controller (see image).

portrait view

I then want to change the position of the views relative to one another when the device transitions to landscape (see image two).landscape view

I want view 4 to move alongside views 2 and 3 and them all the sit below view 1.

Some of the layout conditions:

  • view 1 is attached to the top of the view controller in both landscape and portrait.
  • view 4 is attached to the left, right and bottom margins of the view controller in portrait mode.
  • Views 2, 3 & 4 are centered horizontally within the portrait view.

What is the best method to achieve the different layouts?

Would the most elegant solution be to make a reference to the constraints in the view controllers code and activate and deactivate them in viewWillTransition? Or is there a way to use vary for traits to achieve this (I can imagine views 2, 3 & 4 being centered horizontally would make this hard to achieve, as well as adding the new constraints for view 4 in landscape mode)?


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!