Where is 'vary as traits'? [duplicate] - ios

I can't find Vary for Traits in Xcode 13.
Thanks in advance for any help!

You can set Trait specific variations in the Attributes and Size Inspector panes.
For example, suppose we have this layout:
with Stack View properties:
But -- when we have a Regular Width trait, we want it to look like this:
with these Stack View properties:
Switch back to our original layout (Vertical stack view, spacing: 8, Height: 120).
Select the stack view, and in the Attributes Inspector pane, click the + button to the left of Axis and from the popup select Width -> Regular / Height -> Any:
When we click the Add Variation button, we'll see a new wR variation where we can select Horizontal:
Now click the + to the left of Spacing and again select Width -> Regular / Height -> Any:
When we click the Add Variation button, we'll see a new wR variation where we can change 8 to 32:
Now we select the Height constraint, and in the Size panel click the + to the left of Constant and again select Width -> Regular / Height -> Any:
When we click the Add Variation button, we'll see a new wR variation where we can change 120 to 64:
Now when we switch the orientation in Storyboard / Interface Builder (and when we rotate the device at run-time), we'll get our desired result.

You can Introduce any variations in Layout Constraints

According to this, the only work around is doing the following:
You set constraints for the active size class
Trait Variations need to be enabled in the File Inspector tab
After that you can manually add traits by clicking the plus next to a property (it'll use the active size class but you can modify it
while creating too)
Unfortunately, this only works for properties, and not constraints. There is no other work around for this.
Everyone who has these problems should post bug reports to let the Xcode developers know.

I found one solution, let say you have leading, trailing, top and bottom constraint at 0 for the portrait.
Now in the bottom bar, above the log window, tap to 'Orientation' to put the views in the landscape.
Now find the constraint you want to change for that variation and double click on that constraint, then tap on + icon beside the Constant field and add a new variation, in this example for the landscape. Then change a value and those values will be implemented, one for portrait one for the landscape.

Related

How to set Constraints in Xcode Using StoryBoard?

I am trying to set constraints in iphone devices using storyboard in xcode. But i am getting some issue.I have placed two textfields and two buttons using drag and drop.Everything is going fine except one button whose width get reduced in iphone 5s and i don't know the reason behind this.Can anybody help?
png
Some practical solutions:
add a "width" constraint for that button ( it can be >= also )
adde "equal widths" constraint for that button and another element
adde "trailing and leading" constraint for that button
also
try with different size classes to see your result.
It would also help your question to add more info on how your constraints are setup.
Vandana, if the button width is reduced, it simply means that it does not have a fix width. You have 2 options
1) Set a width constraint on the button (e.g. I want it to be 300 points long)
2) Set a leading and trailing constraint (e.g. on the left I want a fix 10 points distance from the bezel, and on the right I want a fix 10 points distance from the bezel.
I believe there's a warning in the storyboard, you just need to select the best option for you:
Ways to add a width constraint:
1) Option + click and hold "the button"
2) drag to the left or right on storyboard and release while it's on the button:

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 must I do to add a constraint to a view in a Storyboard launch file?

I added a Storyboard launch file to upgrade a project from iOS 4 to iOS 9. The app runs in the Simulator but nothing I do will centre views for iPhone5 and iPhone 6 or scale them to fill the frame. Instead they look like bonsai versions of an iPhone 4 pinned in the top left of the screen.
When I try to add constraints to centre the views by following the procedure described here, the menu will not give me options to choose the view I need to constrain.
Instead I get this.
The bottom two - Horizontally in Container and Vertically in Container - are the only options I can check. But pulling down on the arrow to the right of the text box only gives some of the options needed to Add Constraints
What do I need to do to enable the view option ?
EDIT 1.
And furthermore, when I try to constrain width and height I get this
All six options are in grey and none of the text boxes change
EDIT 2.
And using the Size Inspector to look at the view I am trying to centre and fill, the view has no constraints as shown below.
It is really starting to look like there is no simple way to launch an old app where views have been created programmatically and have Storyboard Launch automatically layout views to suit different iPhone screen sizes.
The "Horizontally in Container" and "Vertically in Container" checkboxes affect individual views. The are "binary operations". If you select more than one view, these checkboxes cause all the views to be centered in their container.
In contrast, the other checkboxes in this popup align groups of views.
If, for example, you select 4 views and click "leading edges" then it creates a set of constraints that line up the leading edges of all the selected views. (Under the covers it creates enough pairs of constraints to line up all the views by their leading edges.)
If you only have 1 view selected then only the bottom 2 checkboxes ("Horizontally in Container" and "Vertically in Container") are meaningful and the others are dimmed. As soon as you select more than one view the others should be enabled.
I had the same problem because on Show the Size inspector > Layout, "Autoresizing Mask" was selected, by changing the value for "inferred (Constrains)" it will resolve this problem
illustration
Select the view you would like to constrain. Then tap the button to the right of the one you have selected and use that menu to define your autolayout constraints. If you want it to work for all devices make sure your storyboard is on the wAny hAny size class setting.

Buttons look different on storyboard to view

Why is my the elements of my storyboard (buttons) showing up differently in the simulator to the device? I've got auto-layout on, and I've disabled size-classes but surely this shouldn't matter?
How it looks in the storyboard: http://s11.postimg.org/h1xbn5f9v/Screen_Shot_2016_02_09_at_18_57_54.png
It looks fine on the iPhone 5 (it matches the storyboard), although on the iPhone 6 and other devices it's different.
How it looks on the iPhone 6:
http://s17.postimg.org/tfdaagt3y/Screen_Shot_2016_02_09_at_18_58_39.jpg
I'm using a view to hold together the buttons and centered it horizontally.
Any ideas? I'm baffled. It seems I'll have to programmatically place it if this is the case...
From the images you've shared thus far, I don't see any constraints other than the height/width. You need to select the buttons of interest and then hold Control and drag to the bottom of the container, click "Vertical Spacing to Bottom Layout Guide" and then on the right panel, set Vertical Space Constant to adjust (incase you find yourself having to temporarily place the button in the container to get the "Vertical Spacing to Bottom Layout Guide" option when Control + Dragging). Looks like you'll need a negative number for the Vertical Spacing constant to achieve the buttons being slightly out the bottom of the container. Then "Update Frames for Selected View" by pressing Control + Option + =
You can also try clicking the little triangle icon and clicking "Add Missing Constraints." This will add missing constraints needed to keep your icons where they are in the Storyboard, but sometimes I've found that using this automatic feature doesn't achieve my exact intent.

Auto layout: Xcode 6: Centering UI elements

I'm using Interface Builder in Xcode 6 to make an app and am having trouble getting the text fields and button to centre on the screen for different size screens.
I thought it was a matter of selecting horizontal and vertical centering in container but it doesn't seem to be that when I try it in auto layout. Actually I've tinkered around a bit and I still haven't got it.
I just want to be able to see all of my button and text fields for any size iPhone screen and right now simulator is only showing part of these UI elements like this:
I also want to do this in storyboard and not in code as I'm not at the level of doing this in code yet.
Step 1: Make sure your size class covers all the iPhone screen at least in portrait view. So, change the size class to "wCompact hRegular".
Step 2: After setting the size class properly, add the UITextFields and UIButton to your storyboard. To me, it looks something like-
Step 3:
Before, you start adding constraints, you need to remember two things-
a. Your element(UITextField, UIButton, UIView or any component) needs to know its starting position unambiguously, and
b. Your element needs to know its size meaning, its height and width.
In this case, as you want to centre your elements, I am just assuming that it needs to be centred starting from 10 scale from the left edge and should end 10 scales away from the right edge of your iPhone screen. Now, that means, it's width will be different based on the screen size, but its height will be same.
So, I just add the constrains following way for the 1st text box-
Notice, in the size inspector, I set the text box's starting point, x and width in a way that it is 10 pt away from left edge and 10 pt away from the right edge. Don't worry, it's just simple math.
For the 2nd textField, I add the constrain, the same way-
Lastly, for the button, the constrains are following-
Now, you are good to go. Everything is centered.
By using your size class selector in the bottom of the storyboard window, set you sizes as any width and any height and then follow the below auto layout constrains. It will work for you.
First select the view you want to set the auto layout, and then select the pin option from the right bottom corner of your storyboard and then add the constrains as shown in above picture and click button Add 4 constrains
Repeat the process for all views and set the constrains as Fix the top, bottom, left and right constrains of all views except the last button that should be fix from top,left,right and fixed height.
You need to make use of the size class selector in the bottom of the storyboard window.
So for an iPhone 6 or 6 Plus in portrait you would choose a compact width and regular height like this:
And then you would do whatever auto layout stuff for the given device there

Resources