Josue Espinosa
Josue Espinosa

Reputation: 5089

Horizontal centering constraint iOS

storyboard image

I have the view controller above with the layout you see. I want to center all of this horizontally. So keep it in its current layout, and when orientation is changed to landscape, have the elements in that layout but centered. How can I do this? I tried using IB to center horizontally but it piled them all in the center without keeping the layout.

Upvotes: 2

Views: 239

Answers (2)

Akshay K
Akshay K

Reputation: 51

The best approach if possible for you to change is to use UIStackView which arranges its subviews in any mode(landscape or portrait) You can refer http://www.appcoda.com/stack-views-intro/ if you are new to UIStackView.

Upvotes: 0

Mazyod
Mazyod

Reputation: 22559

A very quick fix:

  1. Select all your subviews.
  2. Editor->embed in->view
  3. change the autoresizing masks or the autolayout constraints to center that view. Just make sure the view's width and height are constant!

If you are using autoresizing mask: Just remove all the masks as follows:

enter image description here

Upvotes: 2

Related Questions