I have 4 images inside a UIStackView which is constrained 20pts from top, right, bottom, left.
4 images are set to Aspect Fit and the UIStackView settings are set to: Fill Equally, and Spacing of 20.
When I come to view this on an iPhone 8 upwards the layout works. When viewing on an iPhone SE layout I get squashing of the images as below.
Shouldn't the fact that Aspect Fit has been set solve this issue and scale the image down accordingly?
Thanks
Add an aspect ratio constraint to each image and set it to 1:1. This should make the images have the same height and width, no matter the width of the screen.
I agree however, that using "aspect fit" should work. Are these regular images stored in your app bundle, or are you creating them with code or something similar?
Related
I come from design. Using Auto layout to me is like trying to understand Klingon without any previous knowledge of that language. Auto layout defies all logic of good understanding.
I have this situation:
An image view that is 400x400 pixels to be shown portrait and landscape. This image has to be shown with the aspect ratio of 1:1 and adjust itself to be smaller when the device is landscape.
The "image editing" logic says I have to apply the following constraints:
1:1 aspect ratio
leading/trailing of 10 points.
center Y to view
This works on portrait but when I rotate the device to landscape, the image will expand to respect the leading/trailing of 10 points and it will be clipped top/bottom.
Then I thought, I can add a top/bottom constraint to the view >= 10 pt... nope. The image is stretched horizontally and does not respect the aspect ratio of 1:1.
Then I changed the leading/trailing to >=10 and now the image has this:
1:1 aspect ratio
leading/trailing of >=10 points.
top/bottom of >=10 points.
This is working but Xcode is complaining that the image view needs constraints for Y position or height!
For me, Y is defined perfectly based on the leading/trailing/top/bottom constraints and I cannot define height because each device this runs will have one height.
For God's sake. How to solve that?
Couple ways to go about this, but probably the easiest is:
Set Leading, Trailing, Top and Bottom constraints all to 10
Set the ImageView's Content Mode to Aspect Fit
I am currently trying to introduce myself to Xcode, IB and Vector images. I am struggling with the auto layout and having the buttons adjust to screen size.
As you can see the buttons are same size in the iPhone 6s and iPad Pro.
And here is the iPad Pro 12.9" size of the buttons:
The vector images I use are universal .pdf images at the size of 50x41. And here are the settings to those:
How do I get these images to adjust properly from screen size to screen size?
Thanks a lot for your help!
What constraints do you have to change the size of the imageView? I would expect to see a constraint between the top two images (and the bottom two) that forces a constant gap.
The constraints on each image's width should then have a lower priority so they get stretched on a bigger screen. Then you want to constrain the height of each image to its width so they stay 1:1 (I'm guessing), and therefore stretch vertically if they are stretched horizontally.
Set the UIImageView content mode to UIViewContentModeScaleAspctFit and set AutoLayout constraints on the container for aspect ratios rather than just spacing and fixed sizes.
I place an image view (content size: aspect fit) on my base layout. I create the trailing, leading and bottom constraints and set their constants to 0.
This doesn't happen when the image is smaller than the view:
Though, the image isn't positioned properly on the iPhone - it appears almost centered - while it is on the iPad. Perhaps its intrinsic content size is too big? Why is this happening?
As you haven't set a top, aspect ratio, or height constraint on your image view, iOS falls back to the intrinsic content size (the actual size of the image) for the height only (the width is set by the leading/trailing constraints). If the image is wider than the screen, it will result in an image view that is as tall as the original image, but with the with set to the screen width.
Then, as you have the content mode set to Aspect Fit, iOS places your resized image inside the image view, and leaves lots of blank space around it.
One option to resolve this is to add an aspect ratio contraint on the image view, matching the aspect ratio of the image inside it. This will result in a correct height for the image view.
beyowulf was correct i believe. You should try changing the background color of imageview. If you change the imageview background color, you will realise that imageview always obeys your auto-layout constraint.
Now why not image??
Its because you asked it not to :) aspect fit will try to resize the image still maintaining the aspect ratio :) when the size of the image is too large i.e greater than the size of imageview frame, image covers the full frame of imageview and maintains the aspect ratio as well. Meaning if width is greater than screen width, image will cover the imageview frame width and takes a corresponding height for that width.
Thats why in first case your image covers full imageview frame where as in second where image size is small covers only space required :)
If on the other hand you want image to cover imageview frame always either fall back to scale to fill or aspect fill based on your requirement.
Happy coding :)
I am having trouble figuring out how to set up the constraints on an image view.
I have an imageView of size 155*155. I want the view to remain a square shape but scale up if the iPhone screen size increases to 6 or 6+. I can constrain the distance to the bottom of the container and the sides of the imageView to the sides of the main view. This should stretch the width but is there a way to make the height increase by the same amount? Any pointers would be really appreciated. Thanks
You can use an aspect ratio constraint by ctrl + dragging from your image INTO your image.
This basically tells that you want to maintain the current aspect ratio as your other constraints force a change in the width or in the height.
I have to make a new app where the designer make this type of graphics:
iPhone 4:
iPhone 5:
As you can see in the iPhone 4 I have a smaller header than iPhone 5 (also the subviews like the bird and text and smaller). How can I do this with autolayout? I used the aspect ratio without success :/.
Set a vertical space constraint from the top of your UIImageView to the top layout guide.
Then set an Aspect Ratio constraint on the UIImageView. Set the multiplier to whatever ratio you need. For example 4:3. The ratio is width:height. That should do it.
The correct answer is to use proportional height constraint and / or proportional width constraint. See an example: