iOS Simulator/Interface Builder off-center - ios

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.

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.

Some help understanding Interface constraints (xcode)

So I have a text view on a view Controller in xcode and i have constraints that keep it in the center of the screen.
But whenever i run the simulator, I only see about half the text view and the other half is off the screen. Am i just not understanding what constraints are used for? Or am I doing something wrong? How would I essentially keep it in the center of the screen no matter what device screen size is being used?
To correctly center your Text View in the center of the ViewControler using AutoLayout constraints, ensure you are following the two essential configurations as below:
Storyboards in versions of Xcode before 7 had to match the screen size of the target device. This clearly isn’t possible with the “one storyboard to rule them all” approach, so with Xcode 7 the storyboard is given an abstract size instead.
The Use Size Classes option, found in the File Inspector, enables this new format for your project; select the storyboard, open the File Inspector and you’ll see the checkbox option as shown in image in link below:
Once you have ensured the setting 'Use Size classes' is selected as above, use Auto Layout constraints to center the Text View in the View Controller as per image in link below:
(If you need then select vertically in container option as well)

Xcode 6 Do I have to make an application for every screen size?

So I have created my webapplication and when I build it in xcode I have to under "Simulated Metrics" > "Size" set it to 3.5 inch since I have a iPhone 4s.
When I build it and run on the iPhone everything looks perfect, but I want this application to run on iPhone 5/6 aswell, but when I change the size to something else in the Simulated Metrics it gets really messed up on my iPhone. I'm using a webview of my responsive website which shouldn't really care about the screensize but I believe Xcode does. So, is there a solution where xcode automatically detects screensize and makes the webview take the whole viewcontroller? Or do I have to create a application for every screensize?
So, is there a solution where xcode automatically detects screensize
and makes the webview take the whole viewcontroller?
Yes - xcode provides ways to do this. Either through auto layout or using springs and struts. With auto layout you will define a set of rules that your UI will follow. If you want 1 layout for all devices it's fairly straightforward and any tutorial will get you started. with springs and struts you just tell your views how they should grow/stay put depending on screen growing. check this out for a quick introduction to auto layout: http://www.raywenderlich.com/83129/beginning-auto-layout-tutorial-swift-part-1
Or do I have to create a application for every screensize?
Nope.. but you can set different constraints in auto layout for your views to behave very different on different sizes. This gets a little more tricky..
No you do not make a new app for every potential screen size, instead you uses XCode's "auto layout" with a set of "constraints" controlling the size and placement of your widgets. A constraint can be a fixed size for the widget or a given distance relative to another widget (or the parent's border). It is not necessarily a fixed distance, it can also be "my widget needs to be less than 10 pixels apart from this other widget".
The "Simulated Metrics" you refer to in the bottom of the drawing area is only a visual help when laying out you your GUI, it has no effect at runtime. You can safely stick with "Any" width and height.
Read this tutorial to understand XCode's auto layout and constraints:
http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1
Quickly, these are the tools used to setup constraints:
In your case, assuming you have only one big view (your webview), you need to set up 4 constraints stating that the webview's left, right, top and bottom sides are 0 pixels away from the parent's left, right, top and bottom borders respectively:
Lay your webview in the middle of your drawing area
Click on it
Click the "pin" button; you will see this view:
Look at the upper part of the dialog: the small square in the middle represents the view you just clicked and the four red lines represent the distances to the parent's side. Set them all to 0 and click "Add 4 constraints". (Note: the lines are initially dashed when not selected; they turn into solid lines when activated.)
Now the frames in the drawing area do not represent what you were expecting: your webview is still in the middle where you placed it initially and does not fill all the available space; XCode is aware that there is a mismatch between the frames and the constraints and shows a warning. You need to update the frames: click the "issues" button and "update all frames".
The image comes from this question on SO:
What is "Constrain to margin" in Storyboard in Xcode 6
Read it to learn about the "Constraints to margin" switch.

Launch Screen XIB: Missing Width / Height Constraints (Xcode 6)

Using Xcode 6, I am building an iOS app that targets iOS8.
I'm working on a LaunchScreen.xib file, created from the menu: File > New > File > iOS - User Interface - Launch Screen. "Use Auto Layout" is enabled.
Inside the XIB's main UIView, I've placed a UIImageView. I would like to add width and height constraints, so that the UIImageView has the same size as the container. However, Xcode doesn't let me do that: I can only work with spacing and alignment.
Here's what I see:
What am I doing wrong? Why can't I see a menu like the following?
I know I could reach the same result by setting leading space, trailing space, top space, and bottom space to container to 0, but I would also like to understand what's happening here.
EDIT:
Xcode 7+ defaults to a Storyboard file for the Launch Screen.
As explained below, unlike XIBs, Storyboard files allow you to set width and height constraints to the root UIView.
I haven't been able to find an official explanation as to why XIBs behave like that, when they have a UIView at the root.
What looks weird to me is that what we are provided with as a default (LaunchScreen.xib) isn't set up to behave exactly like the Storyboards we've got used to.
The first solution I opted for was using a Storyboard to draw the Launch Screen, instead of a XIB:
Delete LaunchScreen.xib
Create a LaunchScreen.storyboard (Menu: File > New > File > iOS - User Interface - Storyboard)
Go to your project properties, select navigate to your target's general properties and select LaunchScreen.storyboard in the "Launch Screen File" combo box
Open LaunchScreen.storyboard, add a View Controller, and make sure to tick the "Is Initial View Controller Box"
You can now work on the View Controller's View as your Launch Screen, and you'll get the width and height constraints I was initially looking for
An alternative solution is removing the default UIView in LaunchScreen.xib and replacing it with a View Controller.
Just like in the previous case, if you do so, you can work on the View Controller's View, and you'll get the usual constraint menu.
That happens for every .xib, you just can't make a view of equal width than the main view. On the other hand it is possible to do it when you are in a story board.
Finally as you said you can accomplish the desired layout by setting the leading, trailing top and bottom distances.
Will your app run on a single device? If not how do you know the size of the container? It will vary between an iPhone 5, iPhone 6, iPod or iPad. The best way is still to specify leading space, trailing space, top space and bottom space to 0. You're sure it will work on any device.
Anyway, I tried to reproduce the problem, but I am able to specify the width and height of the image view when going in the Pin menu whether in Editor/Pin or by pressing the button at the bottom of the editor. I can specify any width/height with no problem.
Hope that helps!
A very simple solution is to add a clear UIView and pin it to the container, then you can access all the constraints by associating them with the clear "root" UIView you have added.

Xcode 6 Storyboard can't center

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

Resources