Can't set button width in iOS - 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

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:

How to I align 4 buttons together? (autolayout)

I'm trying to align 4 buttons together so they look like this:
I want it so that ONE has a 0 margin to the left, right and bottom, TWO has a 0 margin to the left, right, and bottom, THREE has a margin to the top, left, and right, and FOUR has a 0 margin to the top, left, and right
In order to support iOS 8, I do not want to use stack views
You can add 0 constraint to all their sides, then choose all at same time and give them equal width and height, if custom height then just need to set height constraint to 1 of them, then you can achieve the design
You can set proportion width for all four buttons. Set width = Superview.width and then Set multiplier =0.5 This will make sure that your button width is half of buttons superview so will fit two button in it.
To do so, Right click on Button, Drag to super view and select EqualWidth constraint. then go to constraint and edit multiplier of recently added width constraint to 0.5 (or 1:2).
You can check this video for detail
Use Ctrl+click from one button to other button and choose equal width.All the buttons will have equal width in this way.

Position UILabel in Storyboard

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

Fixed label in that position and equal width

When I was setting a label to equal width inside a scrollview, the label goes up to top instead of staying at the original position. When I remove the constraint, the label is back to position, but without the constraint, the label is not responsive to the screen size.
I already set the scroll view responsive to the view controller. I wished to know the idea of fixing the label position and change the size accordingly to screensize.
Give constrains to your label: left align, right align and Center vertically to superview (in your case ScrollView).
If you can give frame. it will also work else :
Add a top, leading and trailing constraint to the label.
Top constraint will be responsible for your issue of moving to top and left, right constraint will take care of your width of label.

How to Align Two UI Button Horizontally Aligned in A Xib Using Size Class and Auto Layout

How to Align Two UI Button Horizontally Aligned in A Xib Using Size Class and Auto Layout. Two Button should keep its original height and width
i would like to achieve this without code
Thanks in advance
First add gallery button:
Height constaint
Width constraint
Leading space with superview
Top space with superview
Then add camera button:
Horizontal space with Gallery button
Equal width with Gallery button
Equal height with Gallery button
CenterY with Gallery button
And it should be done.
Check this video, to set the constraints
You can check in the preview,
Select both Buttons.
Add the following constraints show in image
This will add default height depend on Text Font.
So if you want to increase height then Select any one button and add extra height constraint. Update the frame.
Just simple select Two Button and Give horizontal and vertical spacing constraints and give Equal Width and Equal Height to Button.
Connect your Gallery button to your main view's corner and set it using
Next, connect your Camera button to Gallery button and set Horizontal Spacing.
Inside your Storyboard's Leading Alignment Contstraint, set Gallery.Trailing as First Item, and Camera.Leading as Second Item. Set Relation to Equal.

Resources