I have an image added to the storyboard with the following constraints:
align CenterY to superview
align CenterX to superview
image width = superview width * 0.43
mode: Aspect Fit
The goal of this is to adapt the image size depending on the screen width.
It does work on iPad, but on iPhone some of the letters of my image are cut off (in this case the top of the O). I tried to make the image larger by adding some space on top of the PNG (transparent pixel). But that didn't changed the behavior. I could use fixed sizes, but shouldn't my approach work too?
Try AspectFill mode instead of AspectFit and set more one constraint is fixed height, whatever height you want. If you want exact square imageview then height should be same as width(i.e. superview width * 0.43).
Now I switched to jpg. Don't know if the transparency or the format (png) was the issue, but for the launchboard iOS has some problems with scaling an image. Especially if there is some curves and transparency.
Related
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?
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.
As the title says, I'm looking for a way to preserve the ration of width:height when one of those two changes.
If you set the image of the UIImageView, the frame gets set accordingly. Say the image is 10x10, so the imageView has a 10x10 frame. If you now shrink the width of the imageView to be 5 wide, the image can appropriately scale to be 5x5, however, the imageView becomes a 5x10 imageView, adding transparent blocks above and below the image.
Currently I've set a height constraint that mimics the ratio of the image I want to set, however, if the image is ever updated or changed to one with a different ratio, it will mess up how the image is displayed.
Is there a generic way to tell the imageView to automatically scale it's width and height to be the same original ratio of the image, or is there any property on UIImageView to allow it to preserve it's width:height ratio when changing only one of the properties?
Use aspect ratio Autolayout constraint.
Set Mode to Aspect Fill.
Check Clip subviews.
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.