I want to create constraints which simply create a vertical space between two views.
First I create it in the wAny hAny size class with a space of 40.
Then I want to have the same constraint with other values in other size classes.
But when I create the same constraint, just with another value Xcode gives me an
auto-layout error that those constraints are colliding.
Because wAny hAny is valid in all other size-classes as well.
In a first attempt I have deleted the constraint out of the wAny hAny size class but then I get the
auto-layout error that the layout is not defined correctly in wAny hAny.
So, it does not matter how I create it, there is always an auto-layout error. I think I just missed a basic concept here to understand what is going wrong. Can somebody explain it to me?
Thanks.
I have found the problem. I was doing it wrong. What I have done is - changing the size-class by selecting the size-class in IB (clicking on wAny hAny at the bottom of the IB screen) and then just set another constant value. This does not work.
This is how it works. Go to the size inspector and click on the plus sign on the left of the constant. Now add the size class. This creates a second constant for the new size class.
Now setting the value on the second constant does work. The original hAny wAny size class is not affected.
Related
I was designing a storyboard in Xcode. Then I wrote a bit of code. When I came back, all my objects in a view had disappeared. Everything's working I just can't see them on my storyboard. But they appear when I run it. I really need to add a uiimageview to this view. But since there are no objects visible I can't. Any help appreciated and
I think this would be the case with you.
Suppose you add label in controller when size class is any other than wAny hAny. Suppose you are adding label in wCompact hRegular size class.An adding constraint for it.
Now you move to other size class. Suppose you change it to wAny hAny.
Now it looks will not find on storyboard and look like this.
In 4th image label looks like hidden and also not visible on storyboard.
So it is better to design storyboard in wAny hAny to make it visible in all size classes. Assuming this will be the case with you.
Did you add constraints to your views?If yes,you changed the size of top level view,just select views and CMD+Option+=(this is resizing view as suggested). If no,add constraints first. If you drag view with blue lines,this will sometimes work,not always.
Select a size class compact in width like "wCompact hAny"
See the width of size class in red box.
What I am trying to achieve
I want to have two views of equal height arranged vertically for all Regular Heights. However, for Compact Heights I want to have the views arranged horizontally. This is illustrated in the following image:
To do this, I added general constraints in the wAny-hAny size class that apply to everything. Then I added specific constraints for the wAny-hRegular and wAny-hCompact size classes. This worked fine for wAny-hRegular and wAny-hCompact (and for testing the app in the simulator), but it left me with missing constraints in wAny-hAny. If I add those missing constraints in wAny-hAny then I get conflicting constraints in either wAny-hRegular or wAny-hCompact. I don't know how to get rid of the errors.
Below is a more detailed explanation of exactly what I did.
How I attempted to do it
I was able to get something that works in the Simulator by doing the following steps.
wAny hAny
For Any Width Any Height I set the following 4 constraints:
That is, I pinned blue to the top and left and I pinned red to the right and bottom. No matter what size class is used, these constraints are always needed.
wAny hRegular
For Any Width Regular Height I added the following 4 constraints:
That is, I pinned blue to the right and red to the left. I also pinned red's top edge to blue's bottom edge. And I made red and blue equal heights. That solves all constraint issues for Regular Height.
wAny hCompact
For Any Width Compact Height I added the following 4 constraints:
That is, I pinned blue to the bottom and red to the top. I also pinned red's right edge to blue's left edge. And I made red and blue equal widths. That takes care of all constraint issues for Compact Height.
The Problem
With the above setup everything works as it should in the Storyboard preview (see first image above) and in the simulator for all sizes that I have tested.
However, in the Interface Builder I am getting the following Missing Constraint errors for the wAny hAny Size Class:
This is a bit of a catch-22, because if I try to add the missing constraints then I create conflicting constraints with either the Regular or Compact Size Class constraints. Here is the Compact as an example:
How do I solve this problem?
I think you need to add specific constraints for the wAny-hCompact size classes only. And make sure the special constraints are correctly installed.
See the image below.
This is because there are two different kinds of constraint for single view in two different size classes. So, rather fixing problem for missing/conflicting Constraints for wAny hAny when using different Size Classes try uninstalling the additional constraint from wAny hAny size class, which were added in wAny hCompact or any other size class.
You can do it like this:
Steps:
If the utilities area is not open, choose View > Utilities > Show Utilities.
Select the view containing the constraint you want to change.
Select the constraint you want to install or uninstall. The Constraint inspector opens in the utilities area.
In the Constraint inspector, click the Add button (+) next to the Installed property and choose the size desired class from the pop-up menu.
After choosing a size class from the pop-up menu, a new entry item for that size class appears in the Constraint inspector.
Note: If there is already an existing entry item for the desired size class, skip this step.
Select the checkbox for for the desired entry line to install the constraint for that size class. Deselect the checkbox to uninstall the constraint.
A runtime object for an uninstalled constraint is still created. However, it is not included in the view hierarchy.
The Constraint inspector shows one or more entry items that show if the constraint is or is not installed for a size class. Each entry line for a size class starts with a Delete button (x) followed by the size class, and then a selection checkbox. The size class is shown for the width (w) and height (h). C is used for the compact size class, R for the regular size class, and Any for the any size class.
You can find detailed explanation over this apple document on installing and uninstalling constraint for size classes.
All you need can be easily achieved with the help of UIStackView that is available from xcode 7 and above .
Apple Documentation of UIStackView
A good Tutorial on UIStackView
I have use the storyboard to create an UI for application. I set the constraints for wCompact hAny and now I want to set different constraints for wCompact hRegular(iphone6+). For this what should I have to do. Please give me any example for this.
You just change you the size and update the constraints accordingly. This will save and be loaded according to the size.
I designed lots of constraints for my iOS app. Now the app works properly on all iPhone in portrait mode. I designed the constraints for the xCompact hRegular class. I realized, unfortunately only now, that all those constraints would be perfect also for all iPad in portrait. So, how can I change the constraints target class without recreating all of them for the more generic wAny hAny class? Thank you.
Select a constraint. In the last property in the attributes inspector, XCode shows you a list of check boxes with the size classes for which the constraint is active. The first check box represents wAny hAny, select it and repeat this for each constraint.
I am not sure how this happened but the view hierarchy on my Xcode6 project have 'hidden' themselves.
On the menu on the left the views have a weird icon and on the layout view and on the app itself the views do not appear.
On the right side all the constraints are greyed out.
Is this a feature or a bug? i can't really find anything i can click to get them to show again. Did i accidentally hit a shortcut?
I think you have changed the size class in the Xcode. May be you have designed it in one size class(not in wAny hAny)and accidentally changed to another. Only the layout designed in wAny hAny will be available for all devices/orientations. If you chosen any specific size class, your view's and constraint's will be hidden for other layouts(grey out). To get it back change size class at the bottom of the IB to yours(i don't know which one you have chosen initially). I would suggest you to design your layout in wAny hAny and do any specific layout changes for a device/orientation using other size classes.
change it here wAny hAny (Interface builder bottom)