Position UILabel in Storyboard - ios

I am trying to position a UILabel within a UIView on a storyboard, without any code. I'd like the label to have an X position of 1/4 of the width of the UIView. So, if the width of the UIView is 400, I want the UILabel to be at X = 100.
Is it possible to do this on the storyboard with constraints without code?

You need to create a constraint such that the leading edge of your label is equal to the trailing edge of your superview with a constant of 0 and a multiplier of 0.25.
To create this in the Storyboard:
control-drag from the label to the right edge of its superview.
Choose Trailing Space to Container from the pop-up.
Now edit the constraint. Click on the I-bar that connects your label to the right of its superview. Open the Attributes Inspector. Choose the First Item pop-up and select Reverse First and Second Item.
The First Item should now be the Trailing edge of your label. Click on that and change it to Leading.
Change the constant to 0.
Change the multiplier to 0.25. (Note: You can also use 1:4 or 1/4 if those seem more intuitive).

First give all other constrains to the UILabel.
to set X position as 1/4 of the width of the UIView
Consider trailing margin of Superview. If trailing margin is 60, then width of the view will be 60

You can use a accessory clear-color view to help get that:
Firstly, create the superview(blue view), and set the constraints
Secondly, you can use a assist view, set it's width is 1/4 of the blue view
1) When you set the assist view's width constraint , you can drag the assist view to the blue view, choose the Equals Width, the in the Size inspector you can edit the width constraint's Multiplier to be 1:4
2) Set the assist view's background color to be clear
The result is this:
The third step, you can drag the label, set the label's left constaint to the assist view

Apart from the answer below what you can do is add a bottom and trailing of your UILabel to your superview. Now control-drag your UILabel to your superview and select the equal width constraint and set the multiplier to 0.75.
The above will allow your your UILabel to always have a width of 3/4 to your superview and since it will have a trailing of 0 to your superview there will always be a space offset by 1/4 the width of your superview

Related

Xcode How to create a square button(width equal to height) inside a Horizontal stackview

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:

Constraints not working

I am not sure why but I have having so much trouble with constrains and auto layout. I have the below view and I want to make it display as is shown but every single approach I take to get seems to be incorrect.
I have been following this tutorial online Auto Layout Tutorial in iOS and I am rather trying to describe my constraints. Following this technique this is what I have:
Description of my constraints.
Label:
Centered in the view
51 from the top
All images:
Must have equal height and equal width.
Are separated from the left and right by a 0 gutter.
All inner gutters separating the images are 12.
I have also included my storyboard.
storyboard link
for this you just declare height and width of the first imageview with respect ViewController use equal heights and equal widths and then change in the multiplier default it will be 1 adjust to your size then for the remaining imageviews you just give equal widths and equal heights of the first imageview.
If you want keep height and width constant , don't pin it, just use horizontal centre constraint. Pin it up using auto layout , hope this is helpful.
You are set image height and set image bottom constraint and does not set label height that you are many choice
Set your label height because you run iPhone 4 or 4s that your label doesn't display.
remove your image bottom constraint because you are set fix image height.
you image doesn't fix height that remove your set image height and all image select and set equal height constraint (all image height same).
Better You should move that four image views into a uiview(childView) .Make sure that 4 image views are the subviews of UIView.
-Add equal width and hight constraint to SuperView from the childView.
- Add multiplier value for the EqualHight and Equal Width constraints by selecting both constraints on storyboard(like 1:2.1,1:2.2).it may keep the same distance in all orientation
Add Y position and X position by trailing space and leading space, centre vertical in container.
Then Select the 4 uiimageviews and make sure they have same width and hight,after that
select 4 image views then click the pin button and check the equal width and equal hight.
Add leading ,top and trailing space for image1
[1,2]
[3,4]
add trailing and top space for image 2
add leading ,top and bottom space for image 3
add trailing ,top and bottom space for image 4,

iOS Auto layout in percentage

I wanna create a simple iOS application using autolayout that displays three UIViews. One on the top, one on the left and
the other on the right. The top view should be 50 pixels high and fills the horizontal space completely. The left column should fill 40% of the horizontal space and should fill 100% of the
vertical space. The right column should be 60% of the horizontal space and 100% of the vertical space. Finally in the left UIView wanna add a single button.
Yes, You can.
Add first view at the top, add four constraint, top 0 left 0 right 0 and height 50.
Add second view at bottom of first view, make it top 0 left 0 heigth 100.
Here is the trick, choose first and secound view, then make them equal width.
Choose secound view's width constraint, and change the multiplier to 0.4.
The two existing answers didn't work for me. My answer was built using Xcode version 10.1 (10B61).
The core question is "How Can I Set a View’s Width to be a Percentage in a Storyboard". Answer: the view's width needs to be associated with the superview.
Explanation:
In the Main.storyboard (already containing a View Controller and a View), add a View. I’m going to call it YellowView). And change its containing view name to SuperView.
In the YellowView's Attributes Inspector change the Background to Yellow.
Click on New Constraints and Top = 0, Leading (left) = 0, height = 50
Control Drag from the YellowView to the Superview and let go, in the resulting dialog select "Equal Widths"
Note: YellowView's width becomes the width of the SuperView.
In the Storyboard, Double-click the width constraint we just added and change the Multiplier to 0.25 (Note: Typing .25 will not work - the leading zero is necessary)
Your constraints should be like,
first view - top,leading(left),trailing(right) and fix height
left view - top, left(leading), bottom and equal width with superview with multiplier 0.4 (or width constraint with 0.4 multiplier)
right view - top,right(trailing),bottom and equal width with superview with multiplier 0.6 (or width constraint with 0.6 multiplier)
hope this will help :)

AutoLayout to dynamically size UILabel Height and Width

I have one UILabel and One UIView contains other subviews side by side in Storyboard. The UIView should butt up against the right edge of the UILabel(trailing constraint of 1), but I also need the UIlabel (the one on the left) to set it's width equal to it's content size unless it hits a max width. Visually:
|Label text| |UIViewWithSubviews|
And I need the following constraints:
1) Label should resize width wise unless it hits a max size and height is also dynamic that means when text reaches the maximum width then the word wrap will break the sentence to another line. and UIview will become vertical to UIlabel.
2) UIView should always be butted up against the right edge of label one in vertical center.
How do I set this up in Storyboard?
Thanks In advance.
Is this what you need?
Here is how i did in Interface Builder.
1) Set a width constraint on the UILabel that is "Less Than Or Equal" to the max width you want.
2) Set the number of lines for the UILabel in the storyboard to be 0. This enables the wrapping you are looking for. Don't set any height constraint at all.
3) Set up constraint to center the UIView vertically with the UILabel.
4) Your other existing constraints sound fine. Just make sure that the UILabel has a clear x and y position, for example by anchoring to the leading and top edges of the superview. And make sure the UIView similarly has sufficient constraints as needed in addition to the vertical centering and the leading edge alignment with the UILabel.

Can't set button width in iOS

I need two buttons with equal width like in following screen. But I can't set its width(half of superview width). Should I set an image to button or how to set width with half of screen size?
Image :
Add constraint to both buttons to be equal width to superview with a multiplier of 1:2 and then snap both to bottom and left button to left and right button to right.
If you use autoLayout, best way is to make both button having a constraint of equal width

Resources