iOS Image View - Height equal to the size of width - ios

I'm just to create an image that spans the entire screens width. The image is a square (512x512) and the idea is to scale to the width of the device while maintaining aspect ratio. I've tried doing this with contraints, setting the margins to left: 0, top: 0, right: 0 and keeping the image at aspect-fill. The issue however, is that the image is becoming larger than the screens width, and thus the image is not only being cut off on the left and right, but is much taller than it should be.
How can I properly size an image to have the same width/height has the width of the device?

The problem is you didn't set constraints on the image view.
Set the following constraints on image view:
1) Set width of image view equal to width of self.view
2) Set aspect ratio 1:1 // This makes sure your image's height is equal to width
You would need additional constraints to specify the x and y position of the image view.

Add constraints as shown in screenShot
Here for Positioning image view, I have given horizontal and vertical center constraints, that you can change according to your requirements(i.e wherever you want to place your imageview.)
Main idea here is to give these two constraints image view -
1.ImageView.width = superview.width
2.ImageView's aspect ratio(width/height) = 1.

Related

Resizing buttons and position on a static background image

I am working to have my buttons height and position adapt to screen size changes like the pictures shown above. The buttons themselves will remain clear and only serve as a simple way to handle taps that trigger the segues to different screens. My goal is to make it so that as the image stretches across different screen sizes, I would like the buttons to keep equal height and width and position with the windows. I know that if the windows had properties I could simply make the buttons have an equal size and width to them and be done, but as I mentioned the image is static and it has to stay that way for the time being. I've tried creating constraints for the buttons and that has only proven to be a headache and I don't know if stack views will help me here either, I know this is fairly complex, but I'm ok with that I just need some direction.
UPDATE: In an effort to follow the instructions LGP listed properly I started from step 1. As I mentioned in the comments, I believe it's simply the ratio and the constraints conflicts since when I remove one or two it works fine, but then how do I set the constraints so it fills the entire screen and maintains the ratio of the picture? Also shown are the constraint conflicts for the image view an it isn't showing the aspect ratio of the parent container view either
If you want to do it in interface builder it is not too hard. You should use spacer views and proportional sizes to position the buttons. That way, whatever size your background will have, all the elements will follow.
1. Create a container that has the same proportions as you image. Add a regular UIView and set an Aspect Ratio constraint with a multiplier of 852:568. This is the dimension of your background photo, 852 x 568 pixels, but the actual values don't matter, as long as the aspect ratio is the same. (Don't forget to also tie up the container view to however you want it in your view controller. See the UPDATE below on how to do this.)
2. Place the background image in the container. Add an image view as a child to the container. Set the constraints to touch all four edges of the container. Set the Image property to you image, and set Content Mode to Aspect Fit.
3. Add the first spacer view. Add a regular UIView to the container view (see leftmost, white view below) and set the constraints as follows:
height = 1 (not important, I used 10 in the image)
Top space to Superview = 90 (not important)
Leading space to Superview = 0
Width equal to Superview with multiplier dw:cw <- This makes it proportional! dw is the distance from the left edge to the first window/button, and cw is the width of the container. If your container is 375 wide, and your distance to the first button is 105, the multiplier will be 105:375.
4. Add the second space view. This is the vertical spacer, going from top to first button. Set it up similar as the first spacer, except make the height proportional to the containers height, and the width fixed.
5. Add the first button. Constrain its left and top edges to the spacers, then make its width and height proportional to the container.
6. Add the remaining spacers and buttons. They are all the same. Just remember where to make them proportional. All buttons are constraint to the single vertical spacer.
Finally, you should make the spacer views hidden. You can easily try it within your Storyboard by selecting different devices.
I chose to add everything on iPhone 8, but it is not really important. Here is what it looks like when I change to iPad Pro 12.9" and iPhone SE. Note how the buttons are positioned correctly. The spacer move around a little because they have partly fixed distances, but it works fine anyway.
UPDATE: Here is how to constrain the container view in the view controller's view to make the container fill the whole view and still keep its aspect ratio.
First, set the image view's (the one you added in step 2 above) Content Compression Resistance Priority to 200 for both Horizontal and Vertical. This will allow the image to compress rather then expand if it has a choice.
Second, Add the following constraints to you container:
Align Center X to Superview
Align Center Y to Superview
Equal Width to Superview >= 0
Equal Height to Superview >= 0
852:568 Ratio to View <- This you should already have!
Now the container will always center on screen, it will always fill at least the entire screen, but will also allow it to fill beyond in both X and Y.
UPDATE 2: If you want to ignore the photo's aspect ratio, and always fill the screen with the photo, you just add constraints for the container view to each side to its superview. Your container view's constraints should look like this.
In step 2 you will need to set the image's Content Mode to Scale to fill. The rest should be the same.
Use percentage based positions and size. Identify the positions of windows in percentage basis, and create the origin in x and y dimension by multiplying the percentage to the width and height of the screen. I am assuming that you are using ScaleToFill as content mode of the ImageView.
Similarly for calculating size, identify the width and height of the ImageView on percentage basis, and multiply the values in percent with the total width and height of the screen.
For example, to calculate the position of Window one-
Suppose, window1.x in percentage basis is 25% & total image view width is 400 (for example), than window1.x pixel position will be-
window1X = (25 * 400) / 100 = 100
Suppose, window1.y in percentage basis is 25% & total image view height is 300 (for example), than window1.y pixel position will be-
window1Y = (25 * 300) / 100 = 75
Suppose, width is 7% of image views width, than width in pixel will be -
window1Width = (7 * 400) /100 = 28
Suppose, height is 12% of image views height, than height in pixel will be -
window1Height = (12 * 300) /100 = 36
window1.frame = CGRectMake (window1X, window1Y, window1Width, window1Height)
Same approach for other windows, to calculate their positions(size will be same as window 1)
This approach will work across all screen resolutions, since it uses percentage based calculations & ScaleToFill as content mode for image view.

constrain for uiimageview to keep aspect ratio and expand based on device size

i have this problem in 2 projects.Normally, profile image is displayed in circle like
and this is ok for iphone5 size.problem raises on bigger devices.like
as you can see size of profile imageview, it looks small compare to cover image.if i let it increase based on outer image then xocde ignores the aspect ratio constrains which results in non cricular profile imageview
here's current constrains image
any suggestion how should i handle it?
Update
here's result if i set constrain on profile imageview to increase with ratio to background image.
asks for y of image view,as i want it to be center so i sets fixed hight = 68 in hopes that xcode will break this constrain to keep aspect ratio, result
so if i remove hight = 68 and add constrain like width.
result
Select your image view and it's super view together and give equal width constraint,
then select that equal width constraint from size inspector, and reduce its multiplier until you get your desired width!
Now select your image view and give aspect ratio constraint!
And finally give position constraint for x and y position!
In your case center x and center y with it's super view I think!
And your imageview will increase or decrease with device width and will remains square always!
And You should set dynamic corner radius(half of width or height of your imageview) in layoutSubviews method of tableviewcell's class as you need rounded imageview and your size is dynamic!
Instead of setting the height and width as constants, set equal width constraints between the background image and the profile image:
And then select the profile image and set the multiplier value to the desired value:
Now you can also set an aspect ratio for the profile image.
This is simple:
Provide Aspect Ratio constraint and make the width of the profile pic = width of the screen
Select the equal width constraint and give the ratio in multiplier

What constraints should be given to increase size of imageview with respect to device screen size without reducing image clarity

I have to show an imageview in a universal app ans it's size should increase as the screen design in every devices without reducing image quality. The image given to the imageview is a static image. If I use static width and height constraints using size classes it will work. But I don't think it as the correct way. If i use width & height
Proportional to the superview, it increases the image height but image clarity reduced in ipads. Any suggestion will be helpfull.
To make Imageviewsize respect to device for universal application , you need to give correct multipler to imageview.
please follow step to get desire output,
Step 1 :- Set Aspect Ratio to your imageview . (you can also set leading and trialing top , bottom etc. and then give aspect ratio.)
Aspect ration required to maintain to set image equal width and Height (Square)
Step 2 :- Set Horizontal center and vertically center imageview . (it will be differ according to your requirement , you can also take this step as step 1 and then set aspect ratio)
Step 3 :- Set Equal height to Superview from imageview.
(Don't panic , you will get warning to update frames or constrain but don't update it)
Step 4 :- Count Accurate Multiplier.
imageviewHeight / superview's height = Accurate Multipliar
Here ,
imageviewheight = 200 , superviewheight=600 so ,
200(imageviewheight)/600(superviewheight)=0.333
(set multipler to imageview where proposanal height = superview)
Step 5 :- check output to different simulator.
I hope this detail explanation is enought to understand your question.
you can give equal height or width with superview with different multiplier. for example multiplier is 0.5 five then your height or width will be half of superview's height.
hope this will help :)
Anchor the edges of the image view as you want. so that the size of image view will change based on device. But set the mode of image view to "Aspect Fit" which will make sure the image will resize but maintain the aspect ratio. You can change mode in Attribute inspector.
Please refer Difference between UIViewContentModeScaleAspectFit and UIViewContentModeScaleToFill?

Xcode 6.3.2 - iOS - Ambiguous UIImageView

I want a UIImageView to expand based on the actual image's height and width. Specifically, I take the image's height and width, and then divide both by ([larger value] / 300) so that the aspect ratio is maintained and the image is optimized to fit in the space I have designed for it (a width that is never smaller than 320 and a height that is always exactly 320). In the code I give the UIImageView the new height and width that I acquired from this procedure. All of this works exactly as I intend it to when viewed in the iPhone Simulator. However, Xcode is giving me warnings about my constraints for the UIImageView, saying that the "position and size" are ambiguous. What is the correct way to make these warnings go away?
Here is the ViewController:
http://i.imgur.com/cVWmVET.png
And here are the constraints I have for the UIImageView in the middle:
http://i.imgur.com/4orUyeh.png
For maintaining aspect ratio and showing image, you don't have to do manual calculations. Just set the following property of UIImageView and assign the image to it
yourimageView.contentMode = UIViewContentModeScaleAspectFit;
For keeping UIImageView in the center of screen, use following autolayout constraints
Constant Height
Constant Width
Horizontal Center in Container
Vertical Center in Container
If you are intending your imageview to expand, then you got to give a bigger values to the Imageview's Content Hugging property,
By Default its 250, give its horizontal and vertical value as 751 as 750 is the default value for Content Compression, so u have to give it a bigger value than that. Tell me if this works.

Adding relative constraints the iOS layout

I want to set relative constraints to layout elements like the image below:
1) the white square must be on the top right corner filling 50% of the horizontal content.
2) the central globe must be relative to the screen size. example: on 3.5 inch device must be smaller, on 5.5 must be greater. like 75% of the screen width.
0) Clear all constraints for sanity.
.
.
1/4 SQUARE
1) Resize your view to kind-of fit the intended size.
2) Select the square view and add margin constraints.
3) multiselect the root view and the view
4) Add equality constraint of width and height.
5) Select the equal height constraint and then the equal width constraint and change the multiplier to 2 for both constraints.
DONE
0) Clear all constraints for sanity.
. .
CIRCLE
1) Put the circle view roughly into the middle.
2) Select it and add these two constraints.
3) This circle is a UIView which is a rectangle so it has width and height that are independent from each other....but we need to keep them equal..so we need to put an Aspect Ratio Constraint.
4) And finally ..we need to give it some size (width == height here). Let's make the size dependent on root view width. Multiselect again both the circle view and the root view..and add the equal width constraint. Also experiment with ratio. 1.6 looks good to me for a start.

Resources