Following is screenshot of my xib :
And this is what appears on simulator :
I don't know why some part of the view is hidden although I have used wAny hAny.
When using auto layout, it's best, in almost every case, to add them yourself, rather than letting the system add them for you -- the system will do that if auto layout is on (Which it is by default) and you don't explicitly add constraints. If you add a view to the canvas, select it, and switch to the size inspector, you will see this message,
The selected views have no constraints. At build time, explicit left,
top, width, and height constraints will be generated for the view.
If you are using wAny hAny, a view that's near the right edge of the view is going to get a constraint to the left side that will be very large, something like 500, so that will be off screen on any phone. You need to add your own constraint to the right side instead.
Related
What does the word "prototyping" mean in the view debugger? It's used to describe a constraint that I didn't add. In IB, the view in question (the upper-left onion image) has an intrinsic width, and its right edge is anchored. It shouldn't need any extra horizontal constraints.
In IB I was designing with a view having simulated metrics set to "Freeform" size, the debugger is showing the view on a 6S. I'm guessing the answer lies in the difference between the two, but can't fathom what's going on.
The added "prototyping" constraint is pulling the onion out of place, towards the left screen edge. Why was it added?
The 'prototyping' constraints are added by XCode Interface Builder when you don't supply enough constraints for it to determine what the layout should be. If you plan to add constraints later at runtime, which it sounds like you're doing, you can add constraints in IB and set them to be removed at build time.
Just add a constraint for the view's leading edge to superview (even though you don't want it), select the constraint in IB, and check Remove at build time.
I was trying to practice Auto Layout in iOS, and I started with very simple UI. Please see image for understanding my problem.
All the text files are in middle of screen (I have deliberately kept on guide line), still you can see in preview, controls are not fully shown. I have not chosen specific size. Size is 'Inferred' still I am not able to see all the controls on UI.
I tried both adding and removing Auto Layout, but no luck. What should I do to create generic UI which will work with all the sizes of iPhone and iPad.
This image is without use of Auto Layout.
After enable autolayout and size classes you have to apply autolayout constraints.
Autolayout is a detail topic. Few basic things when applying autolayout is:
UI element need four constraints.
position x
Position y
height
width
So you will select first label (Number 1). Then press control and drag to superview. You will be provide options. Select Leading space (This will handle x position)
This is the way you can press control and drag:
http://www.appcoda.com/wp-content/uploads/2014/07/auto-layout-login-trailing.gif
Go to size inspector. You can see the constraint.
Press edit and change its value to 25(for test).
similarly control and drag again to superview and select Top space. (This will set y position for label)
This is simple way for the above taken from AppCoda
http://www.appcoda.com/wp-content/uploads/2014/07/auto-layout-control-drag.gif
You can change the value of these constraints according to your need.
UILabel and uitextfield get width and height from their content size. So don't need width and height constraints.
Now when you preview on any device this label will be stick on top left side of screen.
So this is a complete mechanism. You have to apply constraint to every ui element.
Below is a link to very comprehensive tutorial by
http://www.raywenderlich.com/115440/auto-layout-tutorial-in-ios-9-part-1-getting-started-2
At start this tutorial tried to create three views using autolayout. At the end it shows very similar scenario like yours by applying constraints to button and labels.
The problem here is that your constraints are not set correctly to work with every size of iPhone and iPad. You are setting the leading edge constraint to be a fixed size from your view controller's view to the subviews.
The simplest way to solve this issue would be to have a container view that you center in the view controller's view and then use constraints to set 'Center X Alignment Constraint' and 'Center Y Alignment Constraint' to set the container view's center to that of the view controller and then add your subviews to the container view.
As a side note auto layout has a reputation of being hard to learn, you have to put the time in to learn it, I would start with Apple's Auto Layout Guide.
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 am using interface builder to create a masterview details application. However
when I run the app the cell gets misaligned.
Here is how it looks both on the simulator and on interace builder:
Any suggestion on how to fix this?
I have noticed that this happens also in the detail view controller that's why I choose the title of "misplaced view":
The problem in the first screen shot seems to be that you are not supplying a tall enough height for the cell. Thus some of the views on one cell are actually appearing on top of the cell below it. You need to fix your table view's rowHeight.
In the second screen shot, it looks like you are using auto layout but you are not doing auto layout (you have no constraints). You need to position these interface elements with constraints.
Check your constraints in interface builder first and make sure that your UITableViewDelegate is returning the correct heightForRowAtIndexPath:, then report back.
In Interface Builder, if you select one of these views that is not sized correct and then select the "size inspector" tab on the panel on the right (option+command+5), if you have no constraints defined, IB will warn you:
The selected views have no constraints. At build time, explicit left, top, width, and height constraints will be generated for the view.
If you don't see that sort of message, your screen snapshots suggest that you likely have constraints defined for the view which are tantamount to the same thing, left/top/width/height constraints.
The problem is that when you transition to a real device with different width, the layout of the view will not be correct.
If, however, you defined your own constraints (for example, notably using trailing constraint instead of width constraint, using bottom constraint rather than height constraint), you'll find the views will be better adjusted for the device's actual dimensions.
In your first example, iOS 8 will automatically adjust the row height of the cell if you had defined constraints. Something like:
V:|-[nameLabel]-[artistLabel]-[categoryLabel]-[priceLabel]-|
V:|-[imageView]-|
H:|-[imageView(100)]-[nameLabel]-|
H:[imageView]-[artistLabel]-|
H:[imageView]-[categoryLabel]-|
H:[imageView]-[priceLabel]-|
I'm showing it to you in VFL, but you can define it in IB, too. The key point is add constraints so that the vertical height is now unambiguous (i.e. the cell height will be adjusted to fit the labels) and the width is not hard coded, but rather the labels will expand/contract to fill it depending upon the device size.
Conceptually the exact same problem occurs in your second example, that you don't have leading and trailing constraints, but rather IB has defaulted to using leading and width constraints, which will not work as you go from device to device. For example, if you just want the text view, but have it adjust for the size of the device's screen, you might have constraints equivalent to the following VFL.
H:|-[textView]-|
V:|-[textView]-|
Hey, I'd like to obtain what you see in the pictures: in Compact Height mode (landscape iphone) both the red and the blue view have to take all screen vertically and half the screen horizontally. In Compact Width mode (portrait iphone)they have to take all the screen horizontally and half the screen vertically. Space between views should be same size in both modes.
I used to think I have to use size classes and auto-layout constraints, but everything I tried failed miserably.
Maybe I have to use a UICollectionView and change flow direction based on orientation (if that is even possible)?
A collection view is probably overkill, because you don't want scrolling and that's the whole point of a collection view--by the time you do the sizing to stop it you'll have done all the work necessary to set a non-scrolling layout.
This is possible with Size Classes in IB. First, In general you will probably find it helpful to name the views in the Document Outline on the left in IB. You will also want to use this outline rather than try to grab the tiny constraint H-lines.
Set up all the constraints except 1) constraints linking the
OrangeView and BlueView to each other, 2) the constraints linking
the OrangeView to the top and left(leading), and 3) The constraints
linking the BlueView to the bottom and right (trailing).
Change the size class setting at the bottom to w-Compact and
h-Any in the funky box system. Now we're designing for a compact width, so views on top of each other.
Create a constraints for vertical space for BlueView.bottom to
OrangeView.Top. Also create constraint for OrangeView to
superview.leading (or ledaing,margin) and BlueView to
superview.trailing.margin. If you select any one of these constraints and look at the Size Inspector on the right (the ruler) you should see an "installed" checkbox not selected, and below that a w-Compact h-Any and another installed box, this one selected.
Now, while keeping the constraint selected just to see what happens, change the sizeClass selector at the bottom to w-Regular h-Any. Notice that in the Document Outline to the left, it should get grayed out.
Now we are designing for regular, so side-to-side. Add constraints linking the views for horizontal space, BlueView.trailing to OrangeView.leading. Also link OrangeView.top to the superview.top or top aligned to BlueView.top, and same for bottoms. You can manually edit the frame first; if not, IB will automatically fill in the wrong values, so edit these after you create them, and verify they are w-Regular and h-Any. With the ViewController selected, select "update frames" and the views should snap to their expected shape for the size class.
Let us know if this works for you or if it was unclear. Good luck!