当我们有iPhone 4 and 5
时,我们检查了屏幕尺寸,并为每部iPhone制作了2个故事板。
//iPhone 4
if (height == 480)
{
storyboard = [UIStoryboard storyboardWithName:@"StoryboardiPhone" bundle:nil];
NSLog(@"Device has a 3.5inch Display.");
}
//iPhone 5
else if (height == 568)
{
storyboard = [UIStoryboard storyboardWithName:@"StoryboardiPhone5" bundle:nil];
NSLog(@"Device has a 4inch Display.");
}
//iPads
else
{
storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
NSLog(@"Device has a iPad Display ");
}
现在又增加了2部iPhone,问题是,为所有iPhone和iPad制作 5 storyboards
是否正确?在我看来这是一件错误的事情,但我无法找到一种方法在一台设备上安排视图,并使其适合所有其他设备 - 并确保总是工作大。
现在什么是正确的方法?
答案 0 :(得分:5)
最佳方法是使用AutoLayout,但如果由于某种原因仍然需要针对不同的屏幕尺寸使用不同的故事板,则以下是一个正常工作的代码。
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
//iPad
storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
}else{
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone){
// The iOS device = iPhone or iPod Touch
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if (iOSDeviceScreenSize.height == 480){
// iPhone 3/4x
storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone3_4X" bundle:nil];
}else if (iOSDeviceScreenSize.height == 568){
// iPhone 5 - 5s - 4 inch
storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone5_5S" bundle:nil];
}else if (iOSDeviceScreenSize.height == 667){
// iPhone 6 4.7 inch
storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone6" bundle:nil];
} else if (iOSDeviceScreenSize.height == 736){
// iPhone 6 Plus 5.5 inch
storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone6Plus" bundle:nil];
}
}
}
启用原生iPhone 6和iPhone 6 plus屏幕分辨率添加启动图像
答案 1 :(得分:2)
不,您应该使用AutoLayout并编写相应的约束,让系统调整各种大小的UI。
答案 2 :(得分:1)
您应该在Interface Builder中为wAny / hAny“size class”设计UI。应用自动布局约束来描述视图应如何适应不同大小的类。如果需要,可以覆盖特定大小类的某些约束。
应删除您之前选择要根据设备加载的故事板的代码。如果使用大小类,则不再需要它。
有一个出色的WWDC视频,它介绍了自适应UI和大小类。我建议看一下。