"Vary for Traits" not working as expected - ios

From what I understand as explained in the answer to this question you can resize which ever component on the storyboard for any device that you want, well that right there is the where the problem occurs..
I have an image which the regular resolution of it is : 353x229 and this image looks huge on the iPhone 4s screen so I want to resize it to 300x194 only on the 4s screen and this is how I do it :
First I click on the Vary for Traits button, then I click on the image and then click on the "Show the Size inspector" on the right side and from there I change the "Width Equals" to 300 and "Height Equals" to 194 and then I click on "Done Varying".
But the problem is the image is resized for every single device ! Even the iPad ! I don't understand why this is happening and how am I supposed to stop this from happening?

Perhaps by now you have figured it out. But in case others have a similar problem, here's my 2 cents:
I was facing a like problem: a label needed to have a different height for iPhones and iPads. But every time I changed the height of the label on the iPad using vary for traits, it also changed the height on the iPhone.
Here's what I did to fix it:
Select the object (in my case, a label)
In the Size Inspector, double click the constraint you want to change for a different device
Click on the + sign next to Constant.
Select the appropriate traits (in my case, regular height+width for iPad) and add the variation.
Now, underneath the Constant dropdown menu, you will now see your different layout for the corresponding device.
You can now change the size of the respective constraint.
Please note that you have to do this for each constraint you need to change. I realise that it should also work by selecting the desired device and click Vary for Traits. But I think it's buggy and so this is an alternative way of accomplishing the same thing.

When you select vary for traits check the box for width while iphone4 selected.
All devices in blue box will be effected if any constraint is inserted then.
iphone4s screen width is compact in portrait mode and ipad's is regular.
While changing the size dont do it by size inspector rather add constraint of position, height and width.
So after this you need to go back in ipad's mode and select vary for traits and width checkbox, then click somewhere else. Here you will modify the position, height and width in constraints that can be found in size inspector by clicking edit.

Related

How can I set two textboxes in one line for iPad?

I use xCode 8.1.
I have two textboxes in one column for iPhone (in portrait mode):
How can I use adaptive design to display these textboxes in one line at iPad and at Landscape mode of iPhone?
Position your text boxes as you have shown for portrait
Assuming you want Name to stay in the same place, you only need to change the constraints on Surname
You should have a TopSpace and a Leading Space defined for Surname - select each constraint in turn, and click on the + button beside Installed - this gives you the option of introducing variations, so click Add Variation and then de-select the initial default value.
This will mean that these two constraints only apply for iPhones in portrait mode (Compact/Regular
The switch to landscape mode, and position Surname where you need it. Apply constraints - vertical center with Name and a Leading spacing. Select both of those constraints, and repeat the process for creating variations, but they will now be created for iPhone landscape only (Compact/Compact).
Now switch to iPad, and repeat the process you followed for landscape, which will create the iPad constraints (Regular/Regular)
That should be everything you need. If it doesn't work, or you see red lines in the storyboard, it most likely means you have left one of the Surname constraints active for all orientations - at the same time as having conflicting constraints for specifics - if you can't find which one, delete the constraints and start again :-)
Here's a screenshot showing where you add the variation
and here's what it looks like after you have added a portrait-specific constraint
Another way is to use a Stack View with varying trait.
Select the two fields in Interface builder and in the bottom right corner press the Stack button.
Then select the stack view and open attributes inspector tab.
Add a variation based on width, and for this variation (Regular width) set the axis to be Horizontal.
Done.
Note: Don't go with the stack view if your deployment target is bellow iOS 9.
The key to doing this in Xcode 8 is the Vary For Traits button bottom right of the editor.
Select an iPad from the View as list, and set up your constraints as you would for iPad.
Then select an iPhone, and click Vary For Traits, and select width.
Now, select any constraints that aren't used on the iPhone, and hit delete. This doesn't delete them entirely, it just uninstalls them for that size class
Then add the constraints you need for the iPhone. This only adds constraints for the compact width size class
When you are done, click Done Varying
et voila…
[[

What is 'Vary for Traits' in Xcode 8?

I am using AutoLayout and Size classes, but with release of iOS 10 and new Xcode 8.0, there is one new option Vary for Traits. Is this replacement of Size Classe for different width and height of devices.
By selection of width checkbox, it displays varying 14 compact width devices.
By selection of height checkbox, it displays varying 18 compact height devices.
By selection of both checkbox, it displays varying 11 compact width regular height devices.
How to make use of this options ?
Can we use AutoLayout with size classes as like Xcode7.0 ?
If any one has in depth knowledge then please explain it.
This is just an extension as to how to use "Vary Traits" quickly in your project for adding different layouts for iPad and iPhones.
Please read this for understanding more on the Size classes.
https://developer.apple.com/reference/uikit/uitraitcollection
If you are skipping the example which follows below, do read the Summary in the end.
OBJECTIVE :
You need a button having different widths in iPhone and iPad. The former having width of 80 and latter having a width of 300.
METHOD 1 :
Vary for Traits with Multiple Constraints as installed.
STEPS :
Add the common constraints first like Center the button horizontally and vertically.
Choose VaryForTraits and for iPhone screens as per the size class guidelines , a C*R size class fits the model and this we check the tickmarks of Width & Height in PopUp. Dismiss the pop-up by clicking anywhere on screen.
Add the width constant and check whether the constraint is added for C*R size-class. After adding constraints, choose Done Varying button.
For iPad screens, again select any iPad device and choose VaryForTraits and this time on clicking height-width, it should show R*R variation.
Again add a width constraint, the last added iPhone width constraint must be unhighlighted as in the screenshot. The value added will be for the size-class R*R this time.
Switch back to iPhone layout and it takes 80 as width and iPad will take 300.
CONCLUSION :
Please notice that there are total of two constraints added and in both the constraints, the values differ according to the size-class chosen.
METHOD 2 :
Vary for Traits with Single Constraint , Multiple Size-Class installed
STEPS :
Add the normal width constraint. Then select that constraint and choose the + button besides the Constant value.
Add trait variation, and for iPhone we choose C*R and set the constant value as 100.
Again for iPad which follows a trait variation as R*R, we add another variation by clicking again on + button and set the value as 300.
Select an iPad and the width will be automatically taken as 300 and coming back to iPhone it takes 100 as value.
CONCLUSION :
This seems to be a better option rather than adding two constraints when a single constraint is only required and the constant value differs.
WHEN TO USE, WHAT TO USE :
Both the approaches are basically doing the same thing, setting values to Size-classes.
But, #Method1 is used when you want to add a constraint specifically for a device or say size-class. For example, in iPhone the button should be from Top 50 pts and in iPad it should be centered horizontally and vertically. In such situations, you need to use VaryForTraits as it opens doors to add constraints for a specific size class.
#Method2 is used when you want different constant values for a same constraint type.
P.S : TO ALL THOSE WHO ARE NOT ABLE TO GET THE EXAMPLE WORKING
Please make sure, you are adding only the required constraints as Installed. The checkbox against Installed should only appear for the constraint that you need for a size class. That is the key!
Just add a top constraint & a leading to an uiButton in a view. Select the top constraint and uncheck the basic Installed option with Plus sign. Now, by clicking on the Plus sign, add variation to CR and check that option. Now, change the device from iPhone to iPad with various orientation combinations. This constraint will be applied only for CR size class which is iPhone in portrait orientation. If the checkbox against the basic Installed (the one with Plus symbol) was checked that means the constraint should be applied to all size classes.
SUMMARY :
Trait Variation is a change to the presentation of your user interface that is based on a device configuration. Trait Variations of the user interface is not just limited to constraints but can be applied to much more. Such as changing the color of the background and other elements when the device is set to a dark style. A variation can apply to an element of the user interface, such removing a constraint, or to a property of a view class or constraint, such as the font for a label. You can vary:
Size or position of a view
Installation of a view
Installation of a constraint
Constraint constant
Font
Color for the font, tint, or background
Layout margins
Image file
The specific set of properties you can vary depends on the class of the element. In the example, we have demonstrated the use of- Installation of a constraint & - Constraint constant . Others, are quite simple and can be easily inferred.
Vary for traits is the evolution of size classes option that was present in the past version of Xcode. It allows a much more nifty and precise variation based on traits. Of course, it is not limited to iPad/iPhone only variations but you can specify also variations based on orientation and different device.
Other answers in this thread have some lacks and inaccuracies, perhaps the most efficient way to give an answer is to make an example. For the sake of clarity we will limit our example to only a button and two layouts. However, as explained below, you can extend the following example as you wish. Our goal is to to adjust the position of a button between two different layouts: landscape and portrait on all devices.
Note: If not enabled “vary for traits” option, all layout and ui interface adjustments are referred to all traits (ie. all size classes).
Let’s start by putting a button on our storyboard. Since “vary for traits” is not enabled, the button will be present in all different layouts. If, instead, we had enabled vary for traits the button would be referred only to the particular trait selected.
Now, let’s enable “vary for trait” and choose a variation based on height. You should see that the bottom screen will turn in blue and according to the selection you will see all the device impacted. So far, so good.
Select again the button and add the constraints ad usual. In our example, we will add top and left leading space as well as width and height. After that, click on the “Done Varying”. You will see that the bottom part of the screen will turn gray again. What is happening is that we have told Interface Builder to add the above constraints only for the (w:C h:R) classes.
Now select the landscape mode on the bottom of the screen. You will see that the Button is in red, because it lacks the constraints that you have added only for some traits. Select again vary for traits and select again the height variation. Add the following constraints:
and press done varying. Now the button is well identified on the screen both for landscape and portrait.
Build and run. You will see that the button will change according to the orientation of the screen.
You can create more advanced layouts following this pattern. For example, you can select at the beginning a vary for traits and drop UIKit objects only for a specific trait. This object will be present only in the specified variant and will be greyed out on the others one allowing you to create completely different user interfaces based on traits.
It is nothing but size classes it self but with different representation. till xcode 7 we used size classes and we consider height-width in regular,compact and any manner, in vary for traits concept is same but xcode specifically explain exact device. In older version we know that for every iphone in portraint etc kind of information where in this we can know exact device!
Check the below screen shots,
You should refer wwdc2016 - video for more information!
Reference : This So Post

Constrains look different on my iPhone 5S

This is my first post. I just started coding. I want to make my first simple app. I own an iPhone 5S and when I add constrains in compact width / compact width & regular height(in XCode), it shows it right on the iPhone 6S plus simulator. When I try it on my iPhone it just looks weird. I know the sizes of the iPhones are different, but how can I correct this? Now, if I code in compact width / compact width & regular height, it feels like I am coding for an iPhone 6...
I hope someone can help me.
Here is the Storyboard:
I use compact/regular size classes for portrait, and any/compact for landscape, for those two phones you mention (actually, for all of the phones).
And, (squinting at your constraints in the tiny pic), it looks to me like the constraints are "wrong" (not optimal, at least). You've got the button pinned 550 down and 280 across from the edges, which won't look right for smaller devices.
Try aligning the button to horiz and vertical centers, and it will look right everywhere.
Or pin ONE of the edges of the button, and ONE of the top/bottom edges of the button, to the layout margins on one corner. Then it will look right on all devices.
Instead of removing the trailing and bottom constraints, as #Kevin suggests, set both of their constants to 0 (or whatever's reasonable), and make them greater than or equal, not equal.
For the constraints to appear fine for different screen sizes, one possible way is to add the required constraints to your button or label or any other component and have a preview look at it in the assistant editor section.(Click on assistant editor and in the split screen that comes up, find the Automatic section and within it the preview option)This helps to view the layout of your app for different screen sizes. This helps to manage your auto layout issues by a good extent.

Frame "will be different at run time".... Isn't that the whole point?

Maybe striving for a "warning free" project is futile, but I am nervous enough about Adaptive Layout/ AutoLayout that I am compelled to try and squash these warnings.
I have a uiview that I want to be dynamically sized based on the portrait screen width. I have my app set in the Build Info for Universal, Portrait only (and upside down). I have constrained the uiview the following ways:
Aligned the view's Center to Superview's center X
Made the view's width proportional to the Superview's width
Gave the view an aspect ratio constraint of 1:1
Given the view a vertical constraint to the superview's top
For details please see the screenshot below.
So now I get the warnings about the view's "frame being different at run time", in particular the size and position at runtime versus what is in the canvas. Not sure why Xcode "cares" about the canvas where the class sizes are set to Any height and Any width. I thought the whole point of Adaptive layout is that sizes and positions are resolved at runtime based on the devices screen size and orientation.
If I breakdown and let Xcode "Update frames"... yes the warning goes away, but with the super large views it is virtually impossible to work graphically in the tiny canvas in a WYSIWIG fashion. Any solution here to getting rid of the warnings without creating oversized views that make working inside the canvas nearly impossible?
To get rid of the warning, and let the constraints place the views "correctly", just select the view that is giving the warning, press the little triangle thing in the bottom right corner of the storyboard:
And then select "Update frames" for "All Views in Container"
(I wish there was some way of doing this to a whole storyboard. At least I haven't found it)
Select a view and press
Option + Command + =
This will update the frame size. Repeat for all offending views. It doesn't take that long, especially if you find them by clicking on the error messages.
Thanks to this answer for the idea.
Notes
After updating to Xcode 8, and choosing some View as device, I was getting this error. The menu is different than in Xcode 7, so #Spoek's answer was no longer working. Also clicking Update or Reset was not working for me.
Update
Update Frames now has its own button. That's why I missed it before.
You can change simulated metrics size for example to 4-inch iPhone!
Or Freeform and then change the size to what suits you best.
I was getting this error on static tableview cells. I unchecked the Automatic option and the warnings went away

iOS8: class size w:Regular h:Compact To w:Any h:Any

I've made my storyboard support only w:Regular h:Compact.
Cause i needed to support all devices in landscape.
But I've figure that it's not supporting iPad that way.
When i run iPad it just a blank page.
I tried to convert it to any:any but it hide all the sub views.
Is there anyway to convert this class size to any:any to support all without risking redo all this work?
Or at least I need a way to make it only for iPad?
First of all, wRegular hCompact is for iPhone 6 Plus in landscape, not iPads in landscape:
Be sure to check the description at the bottom of the size class choice.
If you want to convert existing elements to show in wAny hAny, try choosing each element, then going to the attributes inspector and clicking the + button at the bottom, then add it for any width and any height. Make sure the checkmark is there beside installed for any width and any height also.

Resources