How to change the height of the view based on width - ios

My views width will change according to the device width but I want to change the height according to that I don't want that to be static.
I couldn't find any constraint that can take care of this.
I heard aspect ratio can do this. But I tried It doesn't seem to work.
How it is generally achieved?

Use concept of aspect ratio with AutoLayout to update your view with respect to height & width. Use multiplier to update aspect ratio. Here are snap of aspect ration assignment and how to update it's multiplier
Logic of Multiplier:
1:1 = Height and width of your image will remain same
2:1 = Your width or height is 2x with respect to each other and create a rectangle. Here Height will be 2x or width will be 2x, it depends upon first item and second item in constraint.
Logic of Priority:
Priority assigns, priority to specific constraint when it conflicts with another constraint of same view. e.g. if you have set a view with 1:1 aspect ratio, with 100 height & 100 width and centred in Main View. Now you also assign Left (margin)/Leading constraint as fix value like 200. In that case for different size of screen width, Centred alignment and/or Leading alignment and/or aspect ration will conflict each other. At this point you need to update priority between 0-1000, according to your requirement for adjustment of design.
Share me your design, I will give your exact logic, how to use this concept

Try with following easy way..
1) Give Leading and Trailing Constraint like below image.
2) Now set Top or Bottom or Other Constraint as per your design. For me i have settled Vertically Center.
3) Now set Height of UIView as per your Ratio which you want in design and after selecting that view just Drag and Drop on same view and select Aspect Ratio. XCode will automatically get Ratio Constraint. For ref. see below image.

Related

Views constrained by width and aspect ratio, not updating height

So on my main.storyboard, I have an image along with a few buttons. All of them are centered. What I wanted to do was scale everything so that it will look exactly the same from a perspective/ratio point of view, on different devices. That is, if an iPhone 8 Plus is 10 percent bigger than an iPhone 8, I want the buttons and image to be 10 percent bigger on the Plus.
I followed this link which told me to constrain the width of the object to a certain percentage of the screen's width (like 25%). Then, he said to constrain the aspect ratio, so that the height will follow along when the width changes based on the screen size.
I did this, for both my buttons and image view. When I look at the dimensions for an object and switch between viewing it through iPhone 8 Plus and iPhone SE, the width changes, but the height does not. So clearly the width is adapting to the screen size properly, but the height isn't. (Note, this is happening for all of the buttons and the image). Should I just constrain the heights and get rid of the aspect ratio constraint? Or is there a way I can fix the aspect ratio so that it does change the height?
Let me know if you have any questions or comments, Looking forward to a response!
EDIT: Per request, here are 2 pictures of the constraints. One is the aspect ratio constraint, the other is the width constraint.
Take a look at this example:
Both buttons have a width constraint of 25% of the view (0.25 * width).
When designing for iPhone SE, they are both 80 x 30
Button B also has an Aspect Ratio constraint of 8:3
When viewed on an iPhone 8+
You see that Button A stretches to a width of 103.33, but its height stays at 30.
Button B stretches to a width of 103.33, but because it has a width-to-height ratio constraint, its height also stretches. In this case, from 30 to 38.67.
For this work you need
1- width constraint to any percentage of screen
2- aspect ratio constraint
by ctrl dragging from the element to itself and select aspect ratio from popup then configure the multiplier , e.x 0.5 mean height will be half width
3- origin constraint x,y

How to use Autolayout to achieve result as per shown screen

I'm developing an app where I'm facing an issue to achieve one UI layout only for iPhone devices. This is the layout that I'd like to achieve:
I want to achieve overlapped views that layout should be same on every device from 5s to 7 plus.
The key is setting proportional constraints.
Start with adding a UIView to hold the 4 elements - background, man, woman and heart.
Add a UIImage for the background, and pin the edges to the edges of the view - that's the easy part.
Add a UIImage for the Man...
Set the Aspect Ratio to 1:1 to keep it square (well, round in this case).
Set the Height constraint equal to the Height of the "containing" view, but then set the multiplier to less than 1 to make it relative to the view. In this case, 0.6 is pretty close.
Set constraints for Centered Horizontally and Vertically... then set the Multipliers to keep the image left and above the centers. 0.64 on horizontal, and 0.9 on vertical work pretty good.
Add a UIImage for the Woman...
Set the Height and Width constraints equal to the Man image.
Set constraints for Centered Horizontally and Vertically... then set the Multipliers to keep the image right and below the centers. 1.4 on horizontal, and 1.2 on vertical work pretty good.
Add a UIImage for the Heart...
Set the Aspect Ratio to 1:1 (or whatever gives you the proper ratio for your heart image).
Set the Height constraint equal to the Height of the "containing" view, but then set the multiplier to less than 1 to make it relative to the view. In this case, 0.15 is pretty close.
Set constraints for Centered Horizontally and Vertically... then it will need a little adjustment o note Vertical 1.05 worked for me.
Now, you just need to set appropriate constraints for the "containing" view, and all the elements will scale and position themselves within it.
When you're all done, it should (hopefully) look like this:
I put the project up in a GitHub repo for you to look at: https://github.com/DonMag/AnotherLayoutExample

Increase aspect ratio according to screen size- iOS

A view at the bottom of a view controller with the following constraint:
leading - 0
trailing - 0
bottom - 0
aspect ratio - 7:1
it looks good on small screen sizes, but on larger screen the view looks too small. What is the best way to increase the aspect ratio according to what screen is using the application
Instead of having aspect ratio set, use proportional height and proportional width. To do so:
1- Select your view and set its height equal to the height of its superview
2- Click Edit and then change "proportional" to whatever you want:
Now you have a view that will be proportional to its superview height. Do the same for its width if you need it.
To be more precise, you can play with the priority of the constraint. For instance keep your ratio constraint and set its priority to 999.
Then change both proportional constraints (those we just add) constant to ">=" instead of "=". Then try different values of multiplier to see how the view react. By doing this you might need a maximum height/width constraint as well (as the view won't know in certain cases which size to choose).

iOS: Constraining View with fixed aspect ratio to trailing edge OR bottom edge

I have an ImageView that is constraint to be a square and on its top edge:
http://i.imgur.com/dACmwPN.png
The problem is that I want the view to fill the entire bottom space without exceeding the superview. In other words it should conform to either the width or the remaining height depending on which one is smaller.
The guide I am following suggests to add all of these to a stack view, but that is an iOS 9 feature and I want to develop for iOS8. What would be a good solution for that SDK?
I would add width = height constraint to keep the aspect ratio, then bottom and trailing constraints with lower priority (900) and >= margin value (ex 8) . This way image will be a square and it will not go over bottom or right. Also aspect fit should be set.

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