Xcode Constraints causing image to enlarge to 1920 x 1920 - ios

Here's a screenshot of the problem:
How it appears in Xcode:
It's got content compression resistance priority of h:750 v:750, content hugging priority of h:251 v:251, trailing / leading space of 5, width and height equals to 390, top space of 40, and a 1:1 ratio.
EDIT: I'm using Auto-layout.
There are other elements that rely on it, but I'd rather get it displaying properly first.
Here's how it looks like in the iPhone 6 Simulator:

You have too many constraints set which conflict with each other. In particular fixing the width and the leading and trailing at the same time will conflict. So will setting the height and the top/bottom conflict. You are probably getting an error in the console telling you it has had to break some constraints.
If you want the image positioned as per your post and to grow with 1:1 to fill either width or height all you should need is:
Set the leading constraint to 5.
Set the top constraint to 40.
Set the trailing to be >= 5.
Set the bottom to be >= 5.
Set the aspect ration to be 1:1
This is saying that you want the image 40 from the top, and a margin of at least 5 on the right and bottom and an aspect of 1:1. The image view will grow to fit based on the constraint which limits its growth: normally the right in portrait or the bottom in landscape.
You should not set the width or height unless you want to fix or limit it. However if you do that you can not set trailing or bottom.
The image inside will show based on how you have set the image view to present the image: apsect fit, aspect fill etc...
Updated
I just noticed you have the image in a scroll view. I think you need to put the image view in a view which you give a defined size then add that to the scroll view. You may need to do that in code to deal with multiple devices. The scroll view needs to know the content size of the content view. See this tech note: https://developer.apple.com/library/ios/technotes/tn2154/_index.html

Related

Image view autolayout to pin 60pt from horizontal edges, but auto scale height?

I'm trying to figure out an approach to the following:
I have a square image, I want to align it horizontally and vertically in the centre of the screen and constrain it in a way where its left and right edges are 60pt from screen edges.
I got thus far, but am now getting an error from XCode about top and bottom image constraints. How can I make it so image keeps its square ratio and auto resizes based on screen size?
Example, images width became 294pt in screenshot below, yet height remained unchanged, where as it should change to 294 pt as well.
You need to add 3 more constraints.
Vertically align your view to superview
Horizontally align your view to superview
Set aspect ratio of your view to 1:1 (Important)
On the whole you should have 5 constraints. The screenshot is given below.
this error occurs when auto layout did not find enough information about a component on the storyboard. as you did it here you only provide the trailing and leading constraint so now it asks for height and width.
if you want the same height on every device selects the height constraints that will fix the heigh 300.
Or simply you can do this set the alignment constraint to Center vertically and horizontally and then add top and leading constraints

issue balancing constraints in Interface Builder when using ratio

I am having a problem with my constraints for an alert view I am building in interface builder.
When the screen is an iPhone 5 size I want the leading and trailing constraints to compress the images and make is smaller, I have a 1:1 ratio constraint set here too. This works great so far. However when the screen gets bigger e.g. iPhone7+ or iPad I want the image to remain the same size (that is the size of the image in interface builder, the iPhone7 size canvas). I have tried setting a constraint on the width of the image using <= and then setting the leading and trailing constraints on the image to priority 750. This works for the bigger screens but now when I go back to the iPhone5 screen size it is broken because the priority on the leading and trailing constraints is less than that of the ratio. The image size remains unchanged. Seems no matter how I work the constraints I can only get the size working for a single scenario iPhone5 or iPhone6+. Has anyone had this kind of issue before? Any suggestions on how I can work the constraints??
Try exploring on size classes. Explore on this link. https://www.raywenderlich.com/162311/adaptive-layout-tutorial-ios-11-getting-started.
Basically by using this you can set separate constraints to a same element with respect to size classes(which in term refers to different sizes of device screens).
You can implement it this way. It works well for iPhones of all sizes.
There are 3 key points regarding the UIView containing the image
Set leading and trailing constraint as greater than or equal (>=)
Center the view horizontally in the superview. This constraint combined with the previous ones will result in the view staying centered even when the image won't be able to stretch anymore.
Set content hugging priority to required (1000).

Xcode autolayout: best fit for square

I have a custom square view that can grow and shrink as needed. My custom view just overrides drawRect to render the content.
The containing view is a rectangle, but not a square. The aspect ratio changes depending on device orientation and type. I need to fit my custom view so that it takes maximum space in the container, but retains the 1:1 aspect ratio.
I would like to set the constraints in IB rather than code if possible. I have the following constraints:
Vertical centering in the container
Horizontal centering in the container
Aspect ratio (1:1)
Content mode set to scale to fill
I tried various things to make the custom view fit the container, which give me IB layout errors, for example:
Width <= container width
Height <= container height
I can't set any of the sizes equal, because I don't know if the container is vertical or horizontal...
This seems like a basic task, but I can't figure this out. What is the best way to fit square in a rectangle?
Keep the constraints you already have, and add new constraints:
width and height equal to the superview, but with lower priorities than the aspect ratio constraint
width and height <= the superview size
first three constraint are right., remove others and set the
leading or trailing and
top or bottom
constraint to superview
Now select the newely added constraint and make them >= (greater than or equal) to 0.
now you'll need to give the width or height constraint to your view which it can have it from its subview's same as we do it for scroll view or give a width equal to 320 to you view and make it also greater than or equal to.

iOS Autolayout: Is the Size Constrains for UIView is Obligatory?

I am trying to arrange controls in the storyboard using constrains via Interface Builder ("Any Width, Any Height" case). I add there UIView, however, when I press update frames for this element (Selected Views case) according to the given constrains UIView disappears from my view. I guess it size becomes zero. Later on, it is not show up in the screen after the run. The issue is also reported in the issue navigator: Horizontal Position of UIView is ambiguous (marked with the green shape). Anyway, if I set the constrain for width or height (size parameters) together with the constrain of ratio, then the issue disappears, I can update frames, it shows up in the screen during the run.
I am adding the picture with the green marks to make the issue more clear:
The question is whether the parameters of the size are obligatory when I am setting the constrains.
Even if they are obligatory how I can make the size to fit different sizes of the screens or iPhone+iPad because if I set/fix the size it could too big for some screens.
The size is not mandatory. The warning tells you that you must set an X position for this view (constraint between the superView left or right border and the left / right border of the view itself). If you want the view to resize itself to fit each screen size you must set both left and right constraint without the size constraint.
You shouldn't used fixed size (width or height) for view that change size when screen dimension change.
If you want view that fix aspect ratio you can set size that relative to Superview's width or height using:
1) leading and trailing and make sure that
view.leading = superview.leading + fixed_margin
view.trailing = superview.trailing - fixed_margin
and not other way around.
2) equal width or equal height with multiplier, constant relative to superview as you want
So you don't need evil size class.

How to correctly use constraints when both UITableView and UIImageView are presented on the same view controller

Suppose that I have the following view controller and this is how I want to see it on all iPhone:
If I run it on iPhone 6 it has the following look:
Here you can notice that UITableView not fit the whole screen and UIImageView doesn't placed at the bottom of the screen.
How can I achieve the required behavior via constraints in XCode 6? I thought that I need the following constraints:
Leading space and top space to container margin for UITableView
Bottom space and trailing space to container margin for UIImageView
Vertical Spacing between UITableView and UIImageView
But it doesn't work as expected even after auto-resolve constraints issues:
Thanks in advance.
Ok, a few things here:
Each view needs enough constraints to define it's x and y position, and it's width and height unambiguously. To start with, go back to Interface builder and delete all of your constraints and lay out the view as you would like it to look. You want to have control over every constraint added, don't let IB automatically resolve the issues, as in all likely hood it won't do what you want.
Do you have an image that is the size you want it to be on screen, once you've factored in #2x, #3x etc? If so, then your job will be easier, as the width and height of the image view can be defined by the width and height of the image (ie the image view's intrinsic content size).
In order to use Autolayout effectively, you need to think about your view holistically, and think about how you want your views to behave when the screen size changes, be clear in your head about the behaviour.
To achieve the layout you want, I would do the following:
Constrain the tableview's leading, top and trailing edges to the superview, with a constant value of 0. This means it can get wider and thinner with the device, it will stretch horizontally, but always stick to the top. This has defined the tableview's x and y position, as well as it's width (height still to go, but keep reading...)
Constrain the image view to match the horizontal centre of it's superview (x position defined) and constrain it's bottom edge to the superviews bottom edge (y position defined). If've you've got the right sized asset, then that will take care of the width and height too. If not, you could go ahead give it explicit width and height constraints.
Now we can constrain the tableview's bottom edge to the top of the image view, with a constant of 0 (ie touching). Note we haven't give the table view an explicit height constraint, so as the device screen grows vertically, the table view will stretch vertically.
Autolayout is hard at first. I'd recommended lots of reading to get over the initial hump, really get to know what a constraint is doing, it's limitations, and the way in which the system parses constraints to translate them into frames. This book is really good, and really helped me learn:
http://www.amazon.co.uk/Auto-Layout-Demystified-Mobile-Programming/dp/0321967194
Best of luck
First make sure you have selected the correct size class. The 'Compact Width | Regular Height' size class must be selected in the Interface Builder. Now add the Trailing space,Leading Space, Top space and Bottom space constraints to the table view. For the image view set the view mode to Aspect fit and add the constraints : Align Center Y ,Top space,Bottom space, Leading space, Trailing space and Aspect Ratio .

Resources