Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto layout: Xcode 6: Centering UI elements

I'm using Interface Builder in Xcode 6 to make an app and am having trouble getting the text fields and button to centre on the screen for different size screens.

I thought it was a matter of selecting horizontal and vertical centering in container but it doesn't seem to be that when I try it in auto layout. Actually I've tinkered around a bit and I still haven't got it.

I just want to be able to see all of my button and text fields for any size iPhone screen and right now simulator is only showing part of these UI elements like this:

View in iPhone 6 simulator

I also want to do this in storyboard and not in code as I'm not at the level of doing this in code yet.

like image 867
cheznead Avatar asked Oct 20 '14 17:10

cheznead


People also ask

How do you align objects in Xcode?

Select the items you want to align, and then click the Align tool. Interface Builder presents a popover view containing a number of possible alignments. Select the options for aligning the selected views, and click the Add Constraints button. Interface Builder creates the constraints needed to ensure those alignments.

What is auto layout in Xcode?

Auto layout will help keep the button central in different orientations and devices. It is a constraint-based layout system that allows developers to create an adaptive interface, that responds appropriately to changes in screen size and device orientation.

What are the different types of auto layout?

There are three main types of Auto Layout issues: ambiguous layouts, unsatisfiable constraints and logic errors.

What is auto layout?

Auto layout is a property you can add to frames and components. It lets you create designs that grow to fill or shrink to fit, and reflow as their contents change. This is great when you need to add new layers, accommodate longer text strings, or maintain alignment as your designs evolve.


1 Answers

Step 1: Make sure your size class covers all the iPhone screen at least in portrait view. So, change the size class to "wCompact hRegular".

Step 2: After setting the size class properly, add the UITextFields and UIButton to your storyboard. To me, it looks something like-

enter image description here

Step 3: Before, you start adding constraints, you need to remember two things-

a. Your element(UITextField, UIButton, UIView or any component) needs to know its starting position unambiguously, and

b. Your element needs to know its size meaning, its height and width.

In this case, as you want to centre your elements, I am just assuming that it needs to be centred starting from 10 scale from the left edge and should end 10 scales away from the right edge of your iPhone screen. Now, that means, it's width will be different based on the screen size, but its height will be same.

So, I just add the constrains following way for the 1st text box-

enter image description here

Notice, in the size inspector, I set the text box's starting point, x and width in a way that it is 10 pt away from left edge and 10 pt away from the right edge. Don't worry, it's just simple math.

For the 2nd textField, I add the constrain, the same way-

enter image description here

Lastly, for the button, the constrains are following-

enter image description here

Now, you are good to go. Everything is centered.

like image 190
Natasha Avatar answered Oct 01 '22 17:10

Natasha