Ryan Shine
Ryan Shine

Reputation: 522

How to make two button center view on the center without cross border?

Have a look at the screenshot

I using stackview to stack the textfield, view, and tableview together. When I doing the constraint on the view,

I set the left constraint is 20, and right constraint as 20 on the both button, more over,

I add the width constraint on the both button but ended up the button show the width is different and cannot looks center on the simulator.

How do I make it to be center?

Upvotes: 1

Views: 130

Answers (2)

Amsheer
Amsheer

Reputation: 7131

I am also new to ios just my suggestion try this.

In storyboard set width constraint for both buttons

Create references for that constraints in your swift code.

@IBOutlet weak var height1: NSLayoutConstraint!
@IBOutlet weak var height2: NSLayoutConstraint!

In your viewwill appear method calculate width using your screen width for example

let screenSize: CGRect = UIScreen.mainScreen().bounds;
let width = screenSize.width;

//Then you need to remove the constraint spaces so

left(10) - Button1 - middle (10) - Button2 - Right(10) So toatal 30

let width_available = screenSize.width - 30;
height2.constant = width_available/2
height1.constant = width_available/2

Edit 1:

The real simple solution just set equal width for both buttons from the storyboard.

Also you need horizontal spacing between the buttons

Upvotes: 0

Adrian
Adrian

Reputation: 16715

I'm working under the assumption you want your view to look like this:

V: textField - viewWithButtons - tableView

For your buttons, I'd highlight them and make them into a horizontal stack. Under attributes inspector, make the alignment fill, distribution fill equally, spacing 8 (or whatever you want).

From there, click your textFieldView, horizontalStackViewiewWithButtons, and tableView and then turn those into a vertical stack. From there, select your verticalStack from the document outline and click the Pin button at the lower-right corner of the screen. Left and right pins are 0, top pin is "Use Standard Value"

From there, work your way "inward" when you add constraints. The outer stack is mostly taken care of. You'll probably want to add a pin for the height of your textFieldView and your horizontalStackViewWithButtons.

Upvotes: 1

Related Questions