Xcode 6 Storyboard can't center - ios

I have an issue trying to center an object in a storyboard. I've created a new "Single-view Application" project, entered a main.Storyboard and I'm trying to add a label to center of it. On a preview of this storyboard for 4-inch iPhone, the label isn't centered, even when I force storyboard to use 3.5, 4, or 4.7-inch iPhones. Adding constraints makes no difference. What am I doing wrong?

If you want to center the label vertically and horizontally, you can use Align option at the storyboard. On your storyboard's bottom right corner, there are four options and the first one is Align option, selecting it will give you to choose the alignment for your object as follows,
Here in the screenshot, you can see that I have selected two options if I want to center my object in both dimension. You can choose either one of them or both based on your requirements and press Add 2 constraints button and it will work consistently for any sized screen. However make sure to select the object you want to center before selecting the Align option.

Try using the reset to suggested constraints. It works quite well for simple layout. Otherwise, you just have to fine tune your constraints

Related

What must I do to add a constraint to a view in a Storyboard launch file?

I added a Storyboard launch file to upgrade a project from iOS 4 to iOS 9. The app runs in the Simulator but nothing I do will centre views for iPhone5 and iPhone 6 or scale them to fill the frame. Instead they look like bonsai versions of an iPhone 4 pinned in the top left of the screen.
When I try to add constraints to centre the views by following the procedure described here, the menu will not give me options to choose the view I need to constrain.
Instead I get this.
The bottom two - Horizontally in Container and Vertically in Container - are the only options I can check. But pulling down on the arrow to the right of the text box only gives some of the options needed to Add Constraints
What do I need to do to enable the view option ?
EDIT 1.
And furthermore, when I try to constrain width and height I get this
All six options are in grey and none of the text boxes change
EDIT 2.
And using the Size Inspector to look at the view I am trying to centre and fill, the view has no constraints as shown below.
It is really starting to look like there is no simple way to launch an old app where views have been created programmatically and have Storyboard Launch automatically layout views to suit different iPhone screen sizes.
The "Horizontally in Container" and "Vertically in Container" checkboxes affect individual views. The are "binary operations". If you select more than one view, these checkboxes cause all the views to be centered in their container.
In contrast, the other checkboxes in this popup align groups of views.
If, for example, you select 4 views and click "leading edges" then it creates a set of constraints that line up the leading edges of all the selected views. (Under the covers it creates enough pairs of constraints to line up all the views by their leading edges.)
If you only have 1 view selected then only the bottom 2 checkboxes ("Horizontally in Container" and "Vertically in Container") are meaningful and the others are dimmed. As soon as you select more than one view the others should be enabled.
I had the same problem because on Show the Size inspector > Layout, "Autoresizing Mask" was selected, by changing the value for "inferred (Constrains)" it will resolve this problem
illustration
Select the view you would like to constrain. Then tap the button to the right of the one you have selected and use that menu to define your autolayout constraints. If you want it to work for all devices make sure your storyboard is on the wAny hAny size class setting.

copied Xcode 5 Button does not correctly center align in IB in xcode 7 for all sizes

I have read through several tutorials and stack overflow posts about this but I can't get it to work.
Here is what I'm doing:
Create fresh Xcode project
Add Button to center of default view in IB
Make sure size is set to Any, Any
click align button in lower right corner to Add New Alignment Constraint
Check box for Horizontally in Center: 0
This works as expected. However, if I copy and paste a button from an existing project, and go through the same steps, I encounter problems.
For example, if I resize to a compact width, the new, copied button is not centered, and instead has a +100.5 next to its constraint.
I have compared the settings of the two buttons and they are the same.
What is the issue here?
I have a number of views from an old project (xcode 5) that I would not like to create, but all elements have this issue.
Just for future reference to this issue, the issue was related to not adding constraints on the second screen. One easy way to do that is by asking Xcode to add suggested constraints for you.
When you drag your button in the middle of screen, you can see that Xcode gives you some hints and shows you horizontal and/or vertical lines when you move the view in the middle of the screen. You can make sure that the view is in the middle of the screen if you can see both horizontal and vertical lines:
And then you can ask Xcode to add missing constraints for you by tapping the first item in the lower right, and choose Add Missing Constraints
For more information on Auto Layout and how to use it, I would suggest checking out Auto Layout sessions in Apple WWDC sessions.

How can I make UITextField always centered on the screen?

I'm making an app in which I add people to a list by using a modal VC, that currently has only three text fields.
Problem:
This is what I see in the interface builder, inside my Main.storyboard file:
I'm running Xcode version 6.2 (6C131e) and the app I'm making will be for iPod touch only, so I disabled auto-size classes. I'm expecting those text fields to be at the center of the screen (same distance from text field lateral border to screen edges). Instead I get this on the simulator:
Question:
How can I set an equal distance for both the sides of text fields? Any resource like tutorials, examples, guides are appreciated, because I am a very beginner in iOS programming. If this can be done with interface builder for me is better then programmatically.
As simple as this:
In the Align menu from the bottom of interface builder if you set this constraint to 0 it will be the center of the view
Select First Textfield
Goto Pin-> Uncheck constraints to Margin-> Select Top,Left,right constraints with default value(or change if want ex. 16 left,right,30 top).
For Second and Third Textfield
Goto Pin->Uncheck constraints to Margin->Select Top,Left,right constraints
If you are not using Autolayout then the default Autoresizing property should be
Change the Autoresizing property to this :
Check this question : Any other method to resize views, other than Auto-layout
I just take horizontally as example. You have two ways to make it in the centre.
1. Make leading space equals trailing space.
Make the textfield center horizontally in container view.
I recommend you can study with raywenderlich tutorial.
http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1

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:
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.
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-
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-
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-
Lastly, for the button, the constrains are following-
Now, you are good to go. Everything is centered.
By using your size class selector in the bottom of the storyboard window, set you sizes as any width and any height and then follow the below auto layout constrains. It will work for you.
First select the view you want to set the auto layout, and then select the pin option from the right bottom corner of your storyboard and then add the constrains as shown in above picture and click button Add 4 constrains
Repeat the process for all views and set the constrains as Fix the top, bottom, left and right constrains of all views except the last button that should be fix from top,left,right and fixed height.
You need to make use of the size class selector in the bottom of the storyboard window.
So for an iPhone 6 or 6 Plus in portrait you would choose a compact width and regular height like this:
And then you would do whatever auto layout stuff for the given device there

iOS Simulator/Interface Builder off-center

In interface builder I placed a button here:
However when I run the iOS Simulator (device: iPhone 5s) it appears here:
I am using Xcode 6 Beta 4
I would recommend you to use size classes due to new iOS screen sizes, but you still can disable size classes on the interface builder in the file inspector as you can see on the image:
If you would like to use Autolayout you can do it adding the next constraints (see the first image) in the corresponding View. In the second step you should use the width and height corresponding to your View. In the second image you can see a recapitulation of all the constraints and a simulator screenshot.
Images:
This is not actually anything to do with size classes. This is due to the fact that it is assuming you have placed it approx. 200 points from the left edge.
What you haven't done is added AutoLayout constraints to say that you actually mean the centre.
CTRL-drag from the button to the view and add...
Center X values
Top space to superview
(Or something like those)
Tha will make it work.
Old question, but still question... You have 2 chooses:
Disable auto-layout
Use auto-layout to center this button on any size of display (go to "Add New Alignment Constrains", check "Horizontally in Container" and then click "Add Constrains")
(Xcode 10.1)
In the small Triangle Menu: Select "Clear Constraints"
Then Select "Add Missing Constraints"
You can also manually adjust constraints by selecting one in the scene.
Then grab the constraint handle.

Resources