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

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

Related

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.

How can I autoresize views according to available space (after rotation) in iOS?

I need to create a layout where 3 views are stacked vertically and the second one must stay centered both vertically and horizontally.
What is happening is that when I turn the phone in Landscape, the first view overlapse the second one. I just want it to resize automatically in order to keep the stacked layout.
You must use the mechanism Size Classes, and set the constraints suitable for vertical orientation of the screen as well as for horizontal. Here is a good guide: tutorial

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).

How to handle different views for different device orientations?

I have a subview in portrait orientation (storyboard scene):
that I want to move and resize like this when the device rotates to landscape orientation:
What the best (or easiest) way to handle this should be?
I'm thinking about the following options:
Creating different separated xib files (one for portrait and another for landscape), and loading the corresponding one?
Rotating + translating + resizing the subview when orientation changes?
Directly creating both subviews (horizontal-bottom one, and vertical-left one) and hidding the corresponding according to the orientation?
I need help with this scenario, I don´t find a solution that works for me. Also if I'm missing any other option, I'll appreciate to know.
Thanks in advance
EDIT: This is an app targeting iOS 7 and above. I'm not using the size classes feature, just autolayout.
EDIT 2:
Setting a new frame to the subview.
What should be the difference of this option with option 2?
Option (2) would be your best bet. Store two sets of constraints and install / uninstall them depending on the orientation.
If you were using size classes, you could do this completely in IB.
(1) is overkill, and (3) is unnecessary.
In portrait mode you have the following constraints for your subview:
Horizontal space constraints for the left, right sides
A vertical space constraint for the bottom side
A height constraint
After the device is rotated to the landscape orientation, you'll change the constraints so that you'll end up with the following constraints for your subview:
Vertical space constraints for the top and bottom sides
A horizontal space constraint for the left side
A width constraint

iOS - Autolayout - Increase/Decrease view size proportionally on orientation change

I went through ray wenderlich's tutorial about autolayouts (link) and then began working on a demo project thinking I've figured it all out but I was wrong. As shown in the screenshot below I have a navigation bar, 2 views and 1 button. The layout on portrait mode looks fine without adding any constraints on views or the button, and understandably in landscape mode views are messed up. I tried adding following constraints on views and those don't seems to work.
View#2: Select view 2 > Editor > Align > Horizontal center in container (hides view completely)
Add Top space to superview. Again view goes away from both landscape and portrait preview.
If I can display view#2 correctly I am planning to add vertical space between view#2 & view#3 and then between view#3 and button#4.
My main concern is to resize the views so that it shows all views and buttons in iPhone 4s landscape mode. Any advise or suggestions are appreciated.
EDIT: Here's the end result that I am trying to get:
The reason your views go away when you add constraints is because a UIView has no intrinsic content size, so its size is {0,0}. The view appeared when you didn't add constraints because the system adds constraints for you, if you don't add them yourself; the system added ones are top, left, width, and height. So, you need to set the size of the views somehow. You can give them explicit size constraints, you can pin them to the edges of the superview, you can give them relative heights based on other views, etc.
Since you want the 2 views to get proportionally smaller in landscape, you should give them heights that are relative to the superview. You do this by selecting the view and the superview, and choosing "Equal Heights" from the pin menu. Edit that constraint to change the multiplier to something like 0.25 for the blue view and 0.2 for the orange one (this assumes that orange or blue view are the first item in the constraint -- if they are the second, then you should use the inverse values of 4 and 5). You should also do the same for the widths, since it seems you want them to get proportionally smaller too.

Resources