When trying to set the constraints of a UIView in the interface builder. When I control drag it gives me the option to set a static height, width, or aspect ratio.
However adding this adds constraints to the width and height.
However I only want the height to be based on an aspect ratio (while the width is equal to the superview width). Can you suggest how I can set a height only aspect ratio constraint?
Leave width and height constrained by aspect ratio.
Select your view and click the "Pin" button at the lower right of the canvas (it looks like a square TIE fighter). In the "Spacing to nearest neighbor" section, add constraints for leading and trailing space to the superview. Set them to -16 if "Constrain to margins" is checked. Otherwise, use zero. These constraints are what will make your view as wide as the superview.
You'll also need to set a constraint that defines the Y position of the view (top space to superview, vertical center in superview, whatever makes sense for your layout).
Related
I have a requirement to create 3 square buttons at bottom of the page. All of them must be aligned and their size should be square.
So far, I have created a horizontal stackview and created autolayout constrains 50 from the left, 50 from the right, 10 from the bottom and set the stackview height as 60.
How can I create square buttons, since the stackview subview width is dynamic and determined at run time?
How can I set an equal aspect ratio for these three buttons?
Don't set the stack view height; let the content determine that.
You haven't said exactly how you want the buttons laid out, but I assume you want them equally distributed.
Put the buttons in a horizontal stack view
Constrain the leading, trailing and bottom edges of the stack view to the superview
Set the stack view's alignment to "Fill" and distribution to "equal spacing"
On each button, create an aspect ratio constraint between its width and its height of 1:1
If you want a minimum height for the buttons, set a >= height constraint on one of them
If you want a fixed height for the buttons, set a = height constraint on one of them
I think the only thing you are missing is setting the Distribution in Attributes Inspector on your Stack View. Set it to distribute equally as shown in the image below:
To set the height, you simply need to set a height contraint on each button. Click the tie fighter looking icon and set the height for each:
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.
As there is super View in that Super there is another View having Height 100 and width 270. i have given constraints like this for inner view (Image) Given Top =5, Leading and Trailing 25 25 and Aspect ratio 320:100(Width of Super View and Height of Inner View), When i add this constraints then size of inner view changes accordingly Means Works Fine. But when I add TextField and Label inside View then Size of label and Textfield Not changes Accordingly Please Specify me what aspect ratio I give To Label and TextField. Please Thank You
Aspect Ratio is used for like as the width increases the height of that particular object also increases with that ratio vice versa case if height increases but giving an object just aspect ratio doesnt mean that the objects dimension will increase/decrease automatically, you have to give other related constraints also to make your object's dimension change as the view's dimension chnage liek for example giving proportional height or width to that object with respect to the view then as the view increases or decreases the dimension of that object will also change.
Give equal height to the container
Set the multiplier
The same you do with textField and with bottom label you give Leading Training equal Height and vertical space with the top label and In textfield also the same.
Click the second UITextField drag it to first set leading ,trailing
,equal height ,equal width.
Then set horizontal space to second label.
This constraints make it proper .
If you want aspect ration drag with in the UITextField set height
constraints and width constraints (or)Aspect ratio Constraints.
I have uiview with constraints for vertical centring inside superview and for width and height. On rotation of device uiview should always be in center, but I need to have ability to change width of uiview but its position should not be changed. I have tried:
- to add leading and trailing constraints but in such case width of uiview is no changing at all
- to add constraints for width that it should be greater and less but in such case width is not changed, uiview just moved
If I am understanding the issue correctly, you probably assigned too many constraints on the view so the autolayout system has a conflict and removes some of the constraints and then the ones you want there don't have any effect. I suggest you look into debug output window to see warnings about constraint conflicts and which ones are being removed.
What you want is: remove all the constraints you added on this view, then constraint its leading to the left side of the super view, constraint it to horizontal center inside superview. This way the uiview will be centered, and will resize accordingly. You can also constraint height or constraint the aspect ratio between width and height. What you definitely don't want is to constraint the height and width directly.
I have fiddled with the constraints for a while and for some reason on a view bigger than the view in the main storyboard looks awful. Right now it's on reset to suggested constraints. Those are my constraints below, I'm not sure what's going on. Is there something wrong with my Xcode or am I doing something wrong?
The button stretches because you pinned it to the top and to the bottom. So on a larger screen the distance from the top and the bottom stays the same which means that the button is stretched.
Keeping the size of the button
The keep the size of the bottom an center is vertically und horizontally you have to give a width and a height constraint and a centerY and centerX constraint:
Keeping the aspect ratio of the button
EDIT: This is a cleaner solution suggested by Sega-Zero:
To scale the button according to the screen size but keep its aspect ratio you have to set an equal width constraint (with a multiplier) between button an its superview, an aspect ratio constraint and centerY and centerX constraints: