问题
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.
回答1:
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.
回答2:
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.
回答3:
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!
回答4:
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.
来源:https://stackoverflow.com/questions/28219842/launch-screen-xib-missing-width-height-constraints-xcode-6