Position is ambiguous for image view - ios

I'm just following the tutorial from Apple, and I get an error when I set the ratio 1:1 to the image view:
I think it's because the tutorial is for xCode 7, so how to fix it ?

From the snapshot you provided, you were not using Stack View, so you probably were following the older tutorial.
I also had this problem when I was following apple's tutorial, and it turned out that I placed the image view out of my Stack View, which gives me the error you mentioned; and more importantly, when I tested the app and chose an image, the image was displayed at its original size.
So the solution for me is simple: placing image view into the Stack View, as shown below:

You need to set all the constraint for the ui elements. Aspect ratio isn't enough, you should set the x and y position too for that image. (e.g.: leading space to View and top space to Button)
If you want to use auto layout, you should allways set all the constraints for all the elements.
EDIT - for better understanding
Just setting the aspect ratio for a view isn't enough. You should give other position and size data too.
In this example, the Leading Space to: Superview set the X coordinate. The Top Space to Top Layout Guide set the Y coordinate. After that we set aspect ratio, and width, or height data for the view. Like the image below.
For better understanding I recommended you to check Stanford iOS developer lessons in iTunesU. Thats will help you understand auto layout.

Related

IOS Stack View Basics

Learning stack view; what's preventing this horizontal line of 60x60 buttons from a) all showing up on both screens (constraints are set on the trailing side so I'm not sure why it's going past the right edge of the screen), and b) scaling the buttons larger. I'm looking to see all the buttons and scale them proportionately to the screen size while maintaining their individual aspect ratios.
Thanks!
1- Put an “aspect ratio “ constraint in each one of them.
2- Put constraints on stack view “leading” and “trailing”.
3- In the attributes inspector for stack view make all views “Equal “ instead of “Fill”.
These setting will make all of them appear with the same size and according to the screen size they will vary but still all of them with the same aspect ratio.

ViewController with proportional view layout

I am wondering if i can achieve this with Xcode's IB auto layout constraints, as currently i am beating my head against a wall, referring to the image below:
I have three views, and want the upper and lower to be proportional to the screen, and have the middle lock x pixels from each. I am sure i can manually do it, but am also sure that somehow the aspect ratio constraint can allow this to happen.
I am pinning the top/bottom view to sides (0), and respective top/botton (o), laying out the view i want in IB's iPad rendering so the proportions look proper, then setting aspect. Logically this makes sense to me, but it isn't working, so i suspect I am making assumptions about the aspect constraint.
Hopefully this is explained well enough to elicit a saving thought or two. thx.
I'm not sure if I understood what you mean correctly, but is that effect below you would like to achieve?
Take a look at the constraints.
Use equal height or width constraints to the superview and set multiplier to the percentage you want it to take. 50% would be .5. To create these in the document outline drag from the child view to the parent view. You will see the option of equal heights or widths. Choose edit on the constraint and change the multiplier from 1 to the desired number. Cheers. Or see answer How to scale height of views and postion of the views relative to the screen size using AutoLayout

Why does Xcode only complain about missing constraints after another one has been set

Help me understand:
When you place any view in the interface builder, Xcode won't complain about missing constraints or ambiguous rules. Only once you set some constraints (but not all to remove every ambiguity), Xcode will notify you of missing constraints and suggest some.
For example: I set a constraint to a view, that centers it horizontally in the superview. Now Xcode complains about a missing rule for the Y position.
Why doesn't it just infer that from the current X-Postition as it does, when you don't have any constraints in place?
That's because before adding constraints to the view, Xcode will see it as if you want the view to be at the exact X,Y position. That means no matter what size the container is, the view will always be at the exact coordinate and have the same size.
However, after adding a few constraints, it means that you want the view to change its size or position according to the container size (which is what autolayout is for), so the constraints you added must provide sufficient information for Xcode to determine its frame.
For example, if you add only Horizontally in Container constraint without specifying its size or Y coordinate, Xcode can't tell where you want the view to be placed. That's why you're getting the warnings.
As for the example you mentioned, if you set the view to be centered horizontally in container, as the width of the container gets larger, Xcode can't tell which one you would prefer:
stay at the same X position and increase the width of the view.
preserve the size and increase X.
Increasing the height of the container will also face a similar problem.
Hope the explanation helps :)
Until you start using constraints, Xcode doesn't know that constraints will be used on that view so it won't show any error. But once a constraint has been added it knows to start applying autoLayout. Xcode now needs to know the width, height, x and y position of that particular view from the constraints. It can either infer them based on the constraints applied on the other views or you can explicitly define them.
Additionally you can use an option that allows Xcode to apply the constraints that it think should be present. But they aren't always what you want. Look in the image below. (The add missing constraints button)
In your example, you applied a constraint specifying where it should be in terms of X-axis but not the Y-Axis. To infer constraints in this case you need to have other views that will have constraints applied on them and this view should have other constraints with respect to these other views that allow it infer its position. In your case, this happens as there is no relation between the X-Axis and Y-Axis. Simply specifying the X coordinate cannot let autoLayout figure out the Y-Coordinate.
Also, for a UIView even if you center it horizontally and vertically, even then it will show an error telling you to define a width and height provided their is no content inside the view

How to have Auto Layout set up a grid of icons proportionally for each device

I am coming across a few issues with Auto Layout when I set up my storyboard.
I want my app display to be identical (image size, spacing, proportions) on each generation of iPhone.
I have somewhat achieved the results that I want by setting each UIImageView to have central Autoresizing and an Aspect Fit Content Mode; however I cannot get the same results once I place the UIImageViews into Stack Views which I have been lead to believe is the proper way to set up this 4x4 icon grid.
Any help is greatly appreciated and if any clarification is needed please let me know.
Stack Views are here to simplify things.
You need 4 horizontal Stack Views inside 1 vertical Stack View.
Then, you need only 4 constraints in the vertical (main) Stack View: Left, Right, Bottom and Aspect Ratio (1:1).
The Stack View configurations are all the same: Fill and Fill Equally.
In my example I also added a space of 10 to each Stack View.
Like this:
Bonus: notice that I'm not using UIImageViews, but custom UIViews,
which draw themselves at runtime. You can even live-preview then in
Xcode via #IBDesignable and #IBInspectable.
Results
iPhone SE:
iPhone 6:
iPhone 7 Plus:
Take a look at the code:
git clone git#github.com:backslash-f/grid-on-stackview.git
(Please give Stack Views a chance. :-))
If the above gives you difficulty, which will not be the case, you can use a container view AutoLayout pinned to the 3 edges. Then place a https://developer.apple.com/reference/uikit/uicollectionview in the container view and set up the collection view with your data source. This is how I solved this problem in the past. Be sure to shut off the scroll, bounce, and control the amount of data source items to perfectly fit the numbers of the grid.

What should I do to make generic size of all UI controls in iOS?

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.

Resources