xcode 4.5 如何在发布时选择故事板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12536689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xcode 4.5 how to pick storyboards at launch
提问by Xaphann
Trying to make my app work with both iPhone 5 and iPhone 4/4s. I tried the "AutoLayout" but does not seem to work for my app also read that it is not supported in iOS 5. AutoLayout specifically fails on a view controller that has an UIScrollview and a UIPicker that is re-sized in code. Having two Storyboards one for 4 inch and one for 3.5 inch seems the way to go.
试图让我的应用程序同时适用于 iPhone 5 和 iPhone 4/4s。我尝试了“AutoLayout”,但似乎对我的应用程序不起作用,还读到 iOS 5 不支持它。 AutoLayout 在具有 UIScrollview 和 UIPicker 的视图控制器上特别失败,该视图控制器在代码中重新调整了大小。有两个故事板,一个是 4 英寸,一个是 3.5 英寸,这似乎是一种可行的方法。
The two Storyboard aproch seem to be the solution for me. So this leaves me with two questions;
两个 Storyboard aproch 似乎是我的解决方案。所以这给我留下了两个问题;
Where should the code to detect then if it is a 4/4s/5 go? I would assume in the appDelegate.m in the didFinishLaunchingWithOptions method
How do I change the "Main Storyboard"?
如果是 4/4s/5,那么要检测的代码应该去哪里?我会假设在 appDelegate.m 中的 didFinishLaunchingWithOptions 方法
如何更改“主故事板”?
回答by fields.cage
This is a great question.
这是一个很好的问题。
What you need to do is,
你需要做的是,
Select your current 4/4s storyboard, go to File, duplicate, then give it an iPhone 5 specific name. Make sure that Target and your app name is checked.
Next you have to select the scenes in your storyboard and in the Attributes Inspector change the size to Retina 4 Full Screen. This allows you to rearrange everything for this display.
Finally in application didFinishLaunchingWithOptions paste the following code with the storyboard name you gave for your 4 inch storyboard.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){ UIStoryboard *storyBoard; CGSize result = [[UIScreen mainScreen] bounds].size; CGFloat scale = [UIScreen mainScreen].scale; result = CGSizeMake(result.width * scale, result.height * scale); if(result.height == 1136){ storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_5" bundle:nil]; UIViewController *initViewController = [storyBoard instantiateInitialViewController]; [self.window setRootViewController:initViewController]; } } return YES; }
选择您当前的 4/4s 故事板,转到文件,复制,然后给它一个 iPhone 5 特定名称。 确保选中 Target 和您的应用程序名称。
接下来,您必须在故事板中选择场景,然后在属性检查器中将大小更改为 Retina 4 Full Screen。这允许您重新排列此显示器的所有内容。
最后在应用程序 didFinishLaunchingWithOptions 中粘贴以下代码以及您为 4 英寸故事板提供的故事板名称。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){ UIStoryboard *storyBoard; CGSize result = [[UIScreen mainScreen] bounds].size; CGFloat scale = [UIScreen mainScreen].scale; result = CGSizeMake(result.width * scale, result.height * scale); if(result.height == 1136){ storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_5" bundle:nil]; UIViewController *initViewController = [storyBoard instantiateInitialViewController]; [self.window setRootViewController:initViewController]; } } return YES; }
If anyone don't get how to do step 1, do as below.
如果有人不知道如何进行第 1 步,请按照以下步骤操作。
Go to Project directory and copy paste the
MainStoryboard.storyboard
and rename new storyboard to sayMainStoryboard5.storyboard
.Add this new storyboard
MainStoryboard5.storyboard
in project (in Xcode) by right clicking Project and clickingAdd Files to ....
Now we have two storyboards in xcode.
转到项目目录并复制粘贴
MainStoryboard.storyboard
并将新的故事板重命名为MainStoryboard5.storyboard
.MainStoryboard5.storyboard
通过右键单击项目并单击,在项目(在 Xcode 中)中添加这个新的故事板Add Files to ....
现在我们在 xcode 中有两个故事板。
Tip
提示
You may have to use 'Product > Clean' for this to work after you have done all the above.
完成上述所有操作后,您可能必须使用“产品 > 清洁”才能使其工作。
回答by Leon Lucardie
Currently the only way is to check if you're using the iPhone 5 is the [UIScreen mainScreen] bounds]
and [[UIScreen mainScreen] scale]
.
目前唯一的方法是检查您是否使用 iPhone 5 是[UIScreen mainScreen] bounds]
和[[UIScreen mainScreen] scale]
。
BOOL isIphone5 = (([[UIDevice currentDevice] userInterfaceIdiom]
== UIUserInterfaceIdiomPhone) && (([UIScreen mainScreen].bounds.size.height *
[[UIScreen mainScreen] scale]) >= 1136));
This only works if you have at least added a [email protected]
launch image to your application. Else this will always return false. (Because the screen will be letterboxed if you don't have the launch image)
这仅在您至少向[email protected]
应用程序添加了启动图像时才有效。否则这将始终返回 false。(因为如果您没有启动图像,屏幕将被黑屏)
To set the storyboard to your iPhone 5 version you might want to take a look at this question
要将情节提要设置为您的 iPhone 5 版本,您可能需要看看这个问题
回答by Tanos
I took inspiration on fields.cage answer and adapted it to my code, it works fine. Thanks !!
我从 fields.cage 答案中汲取灵感并将其改编为我的代码,它工作正常。谢谢 !!
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
UIStoryboard *storyBoard;
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
if(result.height == 1136){
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone5" bundle:nil] autorelease];
}
else
{
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
}
}
回答by Nelson Brian
What i have been doing recently is adding a define statement in any classes i need to check the device. This can also be done in any global header files.
我最近一直在做的是在我需要检查设备的任何类中添加一个定义语句。这也可以在任何全局头文件中完成。
#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
The bool test is from Detect iphone 5 4" screen.
布尔测试来自Detect iphone 5 4" screen。
bool isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136));
if (isiPhone5) {
// Setup For iPhone 5 Screen Size
UIStoryboard *storyBoard;
storyBoard = [UIStoryboard storyboardWithName:@"MyiPhone5StoryboardName" bundle:nil];
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.window setRootViewController:initViewController];
}
This works beautifully if you are already using storyboards, and only want to change the storyboard from the defaults that your project started with for iPhone 5 devices. If you are starting from scratch with an existing non-Storyboard project you can do it this way.
如果您已经在使用 storyboards,并且只想更改您的项目开始时为 iPhone 5 设备使用的默认值,这将非常有效。如果您是从一个现有的非故事板项目从头开始,您可以这样做。
#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
bool isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136));
if (isiPhone5) {
// Load iPhone 5 Storyboard
UIStoryboard *storyBoard;
storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone5" bundle:nil];
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.window setRootViewController:initViewController];
}
else if (IS_IPAD) {
// Load IPAD StoryBoard
UIStoryboard *storyBoard;
storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.window setRootViewController:initViewController];
}
else {
// Load the iPhone 3.5" storyboard
UIStoryboard *storyBoard;
storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.window setRootViewController:initViewController];
}
When i start a project now i design the iPhone 3.5" version in storyboards (If i am using storyboards), then when i am done with that design i go into my project files and find the storyboard file. Since a storyboard file is just an XML layout file, i can take that file and load it in my favorite text editor and change two tags.
当我现在开始一个项目时,我在故事板中设计 iPhone 3.5" 版本(如果我使用的是故事板),然后当我完成该设计时,我会进入我的项目文件并找到故事板文件。因为故事板文件只是一个XML 布局文件,我可以使用该文件并将其加载到我最喜欢的文本编辑器中并更改两个标签。
Convert iPhone to iPad
将 iPhone 转换为 iPad
- At the top of the file find
targetRuntime="iOS.CocoaTouch
" - Change to
targetRuntime="iOS.CocoaTouch.iPad"
- Ad the bottom of the file you may find this
<simulatedScreenMetrics key="destination" type="retina4"/>
- Change this to
<simulatedScreenMetrics key="destination"/>
- 在文件顶部找到
targetRuntime="iOS.CocoaTouch
“ - 改成
targetRuntime="iOS.CocoaTouch.iPad"
- 广告文件的底部你可能会发现这个
<simulatedScreenMetrics key="destination" type="retina4"/>
- 将此更改为
<simulatedScreenMetrics key="destination"/>
The last item will only appear if the your main storyboard file is setup for the 4" iPhone screen.
仅当您的主故事板文件设置为 4" iPhone 屏幕时,最后一项才会出现。
What is important here is if your only adding iPhone 5 to an existing project you only need the first check to override the default, and load your special storyboard file. This literally saved me from having to manually layout all objects in code for iPhone 5.
这里重要的是,如果您只将 iPhone 5 添加到现有项目中,您只需要第一次检查以覆盖默认值,并加载您的特殊故事板文件。这确实让我不必在 iPhone 5 的代码中手动布局所有对象。
回答by BlackSheep
The first one Work very good is simple and clear, but for all if you want to integrate multiply devices look here:
第一个工作非常好简单明了,但如果你想集成多个设备,请看这里:
Create a project for iPhone5 with a Storyboard named iPhone5_Storyboard.
Close app and inside a folder clone the Main Story called iPhone5_Storyboard and rename in to iPhone4_Storyboard not forget to change size and reorder all object inside xcode interface builder.
Optional if you want a Universal version, inside xcode use Add File and adding new Storyboard EMPTY target for iPad, ope un iphone5_storyboard use CMD+A and copy all and paste in the Empty Story for iPad (need to reorder object, but is all linked and working).
In Xcode under TARGET set Universal App and setting under MainStoryboard you iPhone5_Storyboard, under iPad Main Story setting iPad_Storyboard.
Now THANKS TO A FIRST ANSWER OF THIS POSTwe have to implement all different Story using this code inside a AppDelegate.m:
使用名为 iPhone5_Storyboard 的 Storyboard 为 iPhone5 创建一个项目。
关闭应用程序并在文件夹中克隆名为 iPhone5_Storyboard 的 Main Story 并将重命名为 iPhone4_Storyboard 不要忘记更改大小并重新排序 xcode 界面构建器中的所有对象。
可选如果你想要一个通用版本,在 xcode 中使用 Add File 并为 iPad 添加新的 Storyboard EMPTY 目标,打开 un iphone5_storyboard 使用 CMD+A 并复制所有内容并粘贴到 iPad 的 Empty Story 中(需要重新排序对象,但都是链接的和工作)。
在 Xcode 下 TARGET 设置 Universal App 并在 MainStoryboard 下设置 iPhone5_Storyboard,在 iPad Main Story 下设置 iPad_Storyboard。
现在感谢这篇文章的第一个回答,我们必须在AppDelegate.m 中使用此代码实现所有不同的 Story :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
UIStoryboard *storyBoard;
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width *scale, result.height *scale);
//----------------HERE WE SETUP FOR IPHONE4/4s/iPod----------------------
if(result.height == 960){
storyBoard = [UIStoryboard storyboardWithName:@"iPhone4_Storyboard" bundle:nil];
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.window setRootViewController:initViewController];
}
//----------------HERE WE SETUP FOR IPHONE3/3s/iPod----------------------
if(result.height == 480){
storyBoard = [UIStoryboard storyboardWithName:@"iPhone4_Storyboard" bundle:nil];
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.window setRootViewController:initViewController];
}
}
return YES;
}
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
UIStoryboard *storyBoard;
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width *scale, result.height *scale);
//----------------HERE WE SETUP FOR IPHONE4/4s/iPod----------------------
if(result.height == 960){
storyBoard = [UIStoryboard storyboardWithName:@"iPhone4_Storyboard" bundle:nil];
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.window setRootViewController:initViewController];
}
//----------------HERE WE SETUP FOR IPHONE3/3s/iPod----------------------
if(result.height == 480){
storyBoard = [UIStoryboard storyboardWithName:@"iPhone4_Storyboard" bundle:nil];
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.window setRootViewController:initViewController];
}
}
return YES;
}
That's it now we have a universal App for iPhone 3/3Gs/4/4s/5 iPod (AllGen) iPad 1/2/3/4 Mini and Retina
就是这样,现在我们有了适用于 iPhone 3/3Gs/4/4s/5 iPod (AllGen) iPad 1/2/3/4 Mini 和 Retina 的通用应用程序
Thanks guys
谢谢你们