I looked at UIStackView and I don't understand how it can be used with the Resize Class.
I would like to have a imageView and a textView inside stackView in vertical (portrait) mode. I would also like to have the same thing, but in horizontal (landscape) mode.
I can detect the rotation and I change the orientation of the stack. Is this correct?
The best way to change your stack view between portrait and landscape mode using size classes is the following:
Select your stack view in storyboard and look at the attribute inspector:
Click on the plus button next to the attribute you want to change when in landscape and select any width, compact height:
Finally, set the attribute for what you want when the device is rotated for instance:
Hope this answers your question. Cheers!
Related
In my Launch Screen.storyboard, I have a UIImageView named LaunchScreenIcon that scales proportionally to the width of the screen by setting the LaunchScreenIcon.width = width constraint.
Is it somehow possible to change the behavior for landscape orientation so that the height of the UIImageView scales to the height of the screen i.e. LaunchScreenIcon.height = height?
Constrain your image view to 0 on all four sides (so it fills the entire view), then set its Content Mode to Aspect Fit
That should give you what you want.
I was able to accomplish this by:
Going to my Launch Screen.storyboard
Selecting the large iPad model in landscape from the panel at the
bottom
Tapping "Vary for Traits" to the right and checking off both width
and height
Then I added the constraint as I normally would and it was now only
associated with the large iPad model in landscape
I have a view B which comes below view A on Potrait
but when the device changes its orientation to landscape then I want the view B to be horizontally paralell to view A
I am able to do this by adding removing constraints programatically but it seems to be a overkill.
Any solutions on how to do it storyboard itself.
You can use "Trait Variation" in interface builder to specify what the layout should look like when portrait or landscape. https://www.techotopia.com/index.php/Using_Trait_Variations_to_Design_Adaptive_iOS_User_Interfaces
One way to do this is using UIStackView. Add A and B as children, and set the axis to vertical (for portrait) or horizontal (for landscape).
It is possible to do that in storyboard by adding a variation on the Axis property:
I personally would add this variation in code, because all clever storyboard solutions tend to break down and feel as cludges after a while when you need to do maintenance.
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.
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 -
I am using the Interface Builder to create this UI:
And when it is in portrait the One and Two TextViews fill the width of the screen (which is what I want), but when I rotate it to landscape mode the TextViews do not take up the full width of the screen.
How can I get the TextViews to fill the width of the screen in landscape mode? In the Interface Builder, the "Autolayout" option is checked and if possible I would like to accomplish this with it still checked since everything else works best with it on. Any advice would be greatly appreciated since I am still fairly new to iOS programming.
Now the screenshots are posted below. If you notice text box One for example goes all the way to the edge in Portrait view, but not in Landscape view.
If you dont want to use AutoLayout, You can use following steps.
1) In your XIB uncheck autoLayout check box.
2) Select your TextField and set autoResizeConstraints as follows.
Tyr this .... It will help you....
You can either change the textView size programmatically when t change to landscape in the method
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Select your UITextView and press the "Pin" button.
Click the left and right constraints so they turn from dotted light red to red.
Enter your required distance from the superview. In my example, 5 pixels.
Remember, these two constraints are not enough to define a position of the UITextView.
To do this you must either:
Pin the view to either the top or bottom and provide a height
constraint.
Pin the view to both the top or bottom
Center the view vertically in the container
Align the view with some other subview that has a resolvable Y
position