user3186187
user3186187

Reputation: 17

Button always moving to center in xCode

So, I worked through how to move between storyboards, but now I am trying to figure out how button aligning and pinning works... I've set my alignments and constraints already, but for some reason when I build and run the app, the button just goes to the center.

What I mean by this in pictures.

This is getting me really confused, as the restraints seem okay after checking out a few tutorials talking about this, but it just moves to the center and it gets activated with a huge range. What I mean is that I press somewhere not even that close to the button and it immediately moves to the next storyboard, whereas I want it to stick to "Information" button in red.

I'm running it on an iPad Pro as a testing device through xcode's run options.

Thanks for all the help provided so far and I sincerely apologise for all the questions. Hopefully this is the last one...

Upvotes: 0

Views: 226

Answers (1)

Bharath Vankireddy
Bharath Vankireddy

Reputation: 932

Add following constraints

For Button1 (i.e Rocker in your image)

  1. Set Lead, Trail, Bottom spaces
  2. Set Height and width

For Button2 (i.e information in your image)

  1. Set Width and Height

Now select Button1 and Button 2 both and select pin option from the story board (appears at the bottom corner of storyboard (option 3 from left)),

  1. Check option align
  2. from the drop down menu select vertical centers and click on add constraint.

Run your application now. Both buttons will be remains at the same place on any device. Find attached screenshots.

Button 1 constraints

Button 2 constraints

Portrait result

Landscape result

Upvotes: 2

Related Questions