sohan
sohan

Reputation: 99

how to set autolayout for label and button horizontal?

I am trying to set autolayout for a UILabel titled "Have an account?" and a UIButton "sign up" which are placed in a view, not in view controller. The view however is inside the view controller. The spacing between the UIlabel and the UIButton is zero. Both are placed in middle of the view and at the bottom side like this

Have an account ? Sign Up

I have tried the following solutions

step - 1

for label: (1) leading (2) bottom (3) height constraints

step - 2

for button: (1) trailing (2) bottom (3) height constraints

step - 3

Press and drag from UIButton to UILabel and select horizontal spacing.

Here both button and label width are different.

Upvotes: 0

Views: 1267

Answers (3)

Amit
Amit

Reputation: 4886

Better solution is UIStackView.

If you don't want use that then you can do one more thing:

Add a UIView having constraint :

  1. Bottom
  2. Fix width and height
  3. horizontally centre align.

place both the label and signup button in it.

Check this image for reference:

enter image description here

Hope this helps.

Upvotes: 1

RJE
RJE

Reputation: 891

UIStackView is a good way to do this, But this way is also not that hard.

1) Label -> Left, Top, Bottom

2) Button -> Right, Top, Bottom

3) Label - Button horizontal gap

4) Set the height of the container view (not the VC view)

5) Set centre and bottom(Y constraint) of container view within the viewController main view

containerView contains both Label & button and containerView is on View Controller main view

Upvotes: 0

Jad Ghadry
Jad Ghadry

Reputation: 265

You will probably find it easier to use a Horizontal Stack View, set its distribution property to Fill and its alignment property to Center.

Upvotes: 1

Related Questions