change content order when changing orientation with auto layout - ios

I wonder how I can achieve the following with auto layouts in Swift 4 in the story board:
I have a quadratic collection view. In portrait mode, this should be on top filling the entire width and having a height equal to the width. Other controls should then be below it.
In landscape mode, the quadratic view should be on the left, having the height of the landscape and an equal width, thus again being quadratic. Now, other controls should be on the right.
Naturally, the behavior is the opposite: the order of controls is always top -> bottom, no matter the orientation. But for me it is crucial that the quadratic collection view fills as most of the screen as possible.
How can I achieve this behavior?

I found the answer. In XCode10, first set all your constraints, e.g. in portrait mode. Then switch to landscape mode. One can click afterwards click on "View as: iPhone ...", an area will pop up with some button "Vary for Traits".
In the dialog, we can choose to vary based on "Width" (= Landscape) or "Height" (= Portrait), which means that we can define a distinct set of constraints for the orientation that we have so far not utilized.
In the end, we have two set of constraints each for landscape and portrait mode.
This quite extensive tutorial explains more on it: https://www.raywenderlich.com/492-adaptive-layout-tutorial-in-ios-11-getting-started

Related

How to change position of image for portrait or horizontal alignment in XCode 11

I am trying to take an image from filling the right hand side of the screen when viewed in horizontal orientation, to the top quarter when viewed in portrait.
I've followed lots of guides and points on here, but they all related to older version of XCode that seemingly have things XCode 11 don't - such as selecting constant - or just don't work.
I am completely perplexed as to how this should work as when I add "Vary for traits" it applies the move to all devices. I am sure I must be over thinking this, it must be simpler no?
I am using XCode 11.4 with iOS 13.x> above support.
Start by adding a new view controller - use iPhone 8 layout in Portrait Orientation (wC hR):
Add a new UIImageView and position it at the top with about 1/4 the height (doesn't matter how close):
Add top / leading / trailing constraints:
Add a proportional height constraint, at 0.25:
Now change View as to Landscape Orientation (wC hC):
It will look like this (as expected):
Click Vary for Traits, and select Height because we changed from (wC hR) to (wC hC):
Look at the constraints in the Size Inspector, and select This Size Class:
Delete the Leading and Proportional Height constraints:
and add a Bottom constraint and a Proportional Width (0.5) constraint:
And click Done Varying. Now it looks like this:
Notice that we have 2 "grayed-out" (inactive) constraints... If you switch back to View as Portrait Orientation, those two constraint will become "active" and the two (wC hC) constraints will show as "inactive".
Because apps can run in partial screens (iPad multi-tasking), it's advised to no longer think in terms of Portrait and Landscape. Rather, think in terms of size classes / traits.
If your design warrants, you might also add (wR hR) trait variations.
Now, if you really want Portrait vs Landscape, you'll need to use some code. Define a set of constraints for Tall/Narrow layout, and another set of constraints for Wide/Short layout. Then implement viewWillTransition(to size: ... and activate / deactivate the appropriate constraints based on the "to size" width > height or height > width.

iOS Storyboard Constraints/Traits for portrait and landscape view

my iOS project consists at the moment of one single UIView or UIViewController. The controller class is called MainViewController.swift.
The view of the view controller has a subview which I've placed at the bottom:
I've set the auto layout option to keep the distance of the subview for every device size the same:
Now I want to keep the sub view always on the short side of the parent view, which means that in landscape mode the sub view show be on the right side of the parent view:
I've checked some tutorials and other posts in the meanwhile:
Ray Wenderlich Tutorial
Stackoverflow Post
At this point it is not clear to my how to realize such a behavior best?
Possibilites I've found to solve the issue:
Vary for traits...but how?
Constraints?
Programmatically - I think this would be the fastest solution, but I really want to use storyboards and learn the correct usage.
Here you can play with size classes and traits. if you want give constraint in portrait mode you have to select compact width and regular height and for landscape compact width compact height.
Please go through following steps to constraint portrait and landscape separately.
1) Please select mode either landscape and portrait from traits.
2) Now, you can constraint each mode one by one. suppose i am giving constraint to red view in portrait mode and i will enable particular constraints for for portrait size class so it will be disable for landscape mode.
3)once we are finished with portrait mode i have switched to compact height compact regular size class which is landscape mode.
4) Now, we can constraint the landscape mode as shown in above screenshot.
As we can see constraints which is light in color are disable for current size class.
So this is how we can constraints landscape and portrait mode separately.
Because there is no size class or trait difference between an iPad in landscape, and an iPad in portrait, you will ultimately have to handle at least some cases programatically. That being the case, you might as well just handle all cases programatically with a method that does something like:
Get current screen size
If width > height, add constraints to center the subview vertical and pin it to the right edge. Otherwise, add constraints to center the subview horizontally and pin it to the bottom edge.

Setting proper Constraints to UIView in Portrait and Landscape

This is the first time I'm making an iOS app in both orientations.
I have a UIView placed like in above image.
I have set the following constraints: Horizontally Centered, Vertically Centered, Leading and Trailing Space : 8, Height : 265.
I want this UIView to look fullscreen in landscape mode such that Leading, Trailing, Top and Bottom is stuck to all edges.
Please help me in doing it. The above set constraints are not looking proper in landscape. See image below :
The above screenshots are of iPhone 6s size. I need app to support iPad in both orientations too.
These are the constraints already applied by me :
EDIT : After vary for traits is applied :
Portrait constraints are the active one and greyed out ones are applied to landscape. It looks as I want in storyboard but still taking height of portrait during runtime.
EDIT 2: Vary for traits is working on that particular size class but not on all size classes. Please suggest how to set same constraints on all size classes.
select landscape option (below the screen near to setting auto layout pane) then press vary for traits , now give new constraints that you want in landscape mode then select done varying.
Now you have two different constraints for landscape and portrait mode.
Add below constraints just change the value of constant according to your requirement -
Check below -

Switching to landscape, auto constraints in wrong corner

I'm presenting a view controller when my view controller is rotated to horizontal.
In my NIb, I have a help button with constraints to margin top and right of 8 pixels each. However, my view controller displays with the button in the top right of a Portait perspective. See image. How do I make this show up in top right in Landscape?
It looks like your screen did not rotate, because the status bar is still in portrait mode. BUT... even if it did rotate, you would probably have problems because the status bar is 8 pts wide, and it is automatically removed in landscape mode.
I'd say (1) set your constraints to put the button 30 or 40 pts from the corner, until you get the rotation stuff worked out, and (2) fix the rotation issues by looking at the settings for which rotations the simulator supports. Normally rotations allowed by xcode (at least for my default swift projects) are left, portrait, landscape, but not upside down.
It happens to me all the time, when using the simulator!
(Assuming that your project is supporting both landscape and portrait mode)
To make sure your device has rotated from landscape to portrait or the other way around, simply rotate it three times instead of once! (Press the ⌘ and then → three times)
Also for your button placement, I would recommend creating a Standard offset to superview's top and trailing margins, without using any particular numbers (Standard pops up when you delete the number in the constraint's Attribute Inspector).

iOS Auto Layout - How to have two stacked views in portrait move to side by side in landscape?

I am updating my app and want to allow landscape orientation.
I am wondering how I can take two views that are stacked on top of each other (and centered horizontally in container) while in portrait and move them to be side by side in landscape (and centered vertically in container).
I would like the bobblehead to be aligned on the left, and the quote box to be aligned on the right. Both views should take up full height while in landscape.
Here are some screen grabs:
What you want is having different layout when device size is not fit to your view.
In iOS8 you can do it without hard coding it.
Yes just by designing right constraint for right size class(introduce in XCode6)
Here is an example which doing same as you want
Example
Code
Download project
Output

Resources