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

前端 未结 2 1447
耶瑟儿~
耶瑟儿~ 2020-12-09 13:04

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

I then want to change the position o

2条回答
  •  天命终不由人
    2020-12-09 13:31

    We used to set up different sets of constraints and activate/deactivate them based upon orientation change. But nowadays can use size classes and "vary for traits".

    For example, I start with a simple view and choose a compact width size class and then choose "Vary for traits":

    I then add the appropriate constraints and click on "Done varying":

    I then choose a "regular width size class" and repeat the process ("Vary for traits", add the constraints, click on "Done varying":

    You then end up with a scene that will have a completely different set of constraints active for compact width size classes and regular width size classes. I.e. when I run the app, if the device rotates, the new constraints are activated:

    For more information, see WWDC 2016 videos on adaptive layouts:

    • https://developer.apple.com/videos/play/wwdc2016/222
    • https://developer.apple.com/videos/play/wwdc2016/233

提交回复
热议问题