ios 如何为 iPhone 5 屏幕分辨率开发或迁移应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12395200/
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
How to develop or migrate apps for iPhone 5 screen resolution?
提问by Lukasz
The new iPhone 5 display has a new aspect ratio and a new resolution (640 x 1136 pixels).
新的 iPhone 5 显示屏具有新的纵横比和新的分辨率(640 x 1136 像素)。
What is required to develop new or transition already existing applications to the new screen size?
开发新应用程序或将现有应用程序转换为新的屏幕尺寸需要什么?
What should we keep in mind to make applications "universal" for both the older displays and the new widescreen aspect ratio?
我们应该记住什么才能使应用程序“通用”适用于旧显示器和新宽屏纵横比?
采纳答案by Filip Radelic
- Download and install latest version of Xcode.
- Set a Launch Screen File for your app (in the general tab of your target settings). This is how you get to use the full size of any screen, including iPad split view sizes in iOS 9.
- Test your app, and hopefully do nothing else, since everything should work magically if you had set auto resizing masks properly, or used Auto Layout.
- If you didn't, adjust your view layouts, preferably with Auto Layout.
- If there is something you have to do for the larger screens specifically, then it looks like you have to check height of
[[UIScreen mainScreen] bounds]
as there seems to be no specific API for that. As of iOS 8 there are also size classes that abstract screen sizes into regular or compact vertically and horizontally and are recommended way to adapt your UI.
- 下载并安装最新版本的Xcode。
- 为您的应用程序设置启动屏幕文件(在目标设置的常规选项卡中)。这就是您如何使用任何屏幕的完整尺寸,包括 iOS 9 中的 iPad 拆分视图尺寸。
- 测试您的应用程序,希望不要做其他事情,因为如果您正确设置了自动调整大小蒙版或使用了自动布局,一切都应该神奇地工作。
- 如果没有,请调整您的视图布局,最好使用自动布局。
- 如果您必须专门为较大的屏幕做一些事情,那么您似乎必须检查高度,
[[UIScreen mainScreen] bounds]
因为似乎没有特定的 API。从 iOS 8 开始,还有一些尺寸类可以将屏幕尺寸抽象为垂直和水平方向的常规或紧凑尺寸,并且是调整 UI 的推荐方法。
回答by Sanjay Chaudhry
If you have an app built for iPhone 4S or earlier, it'll run letterboxed on iPhone 5.
如果您有为 iPhone 4S 或更早版本构建的应用程序,它将在 iPhone 5 上运行。
To adapt your app to the new taller screen, the first thing you do is to change the launch image to: [email protected]. Its size should be 1136x640 (HxW). Yep, having the default image in the new screen size is the key to let your app take the whole of new iPhone 5's screen.
要使您的应用程序适应新的更高屏幕,您要做的第一件事是将启动图像更改为:[email protected]。其大小应为 1136x640 (HxW)。是的,在新的屏幕尺寸中使用默认图像是让您的应用程序占据整个新 iPhone 5 屏幕的关键。
(Note that the naming convention works only for the default image. Naming another image "[email protected]" will not cause it to be loaded in place of "[email protected]". If you need to load different images for different screen sizes, you'll have to do it programmatically.)
(请注意,命名约定仅适用于默认图像。将另一个图像命名为“[email protected]”不会导致它被加载到“[email protected]”的位置。如果您需要加载不同的图像对于不同的屏幕尺寸,您必须以编程方式进行。)
If you're very very lucky, that might be it... but in all likelihood, you'll have to take a few more steps.
如果您非常幸运,那可能就是这样……但很可能,您必须多走几步。
- Make sure, your Xibs/Views use auto-layout to resize themselves.
- Use springs and struts to resize views.
- If this is not good enough for your app, design your xib/storyboard for one specific screen size and reposition programmatically for the other.
- 确保您的 Xibs/Views 使用自动布局来调整自己的大小。
- 使用弹簧和支柱来调整视图的大小。
- 如果这对您的应用程序来说还不够好,请为一种特定的屏幕尺寸设计您的 xib/故事板,并以编程方式为另一种屏幕重新定位。
In the extreme case (when none of the above suffices), design the two Xibs and load the appropriate one in the view controller.
在极端情况下(当以上都不满足时),设计两个 Xib 并在视图控制器中加载适当的一个。
To detect screen size:
检测屏幕尺寸:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
????CGSize result = [[UIScreen mainScreen] bounds].size;
????if(result.height == 480)
{
// iPhone Classic
????}
????if(result.height == 568)
{
// iPhone 5
????}
}
回答by onegray
The only really required thing to do is to add a launch image named "[email protected]" to the app resources, and in general case (if you're lucky enough) the app will work correctly.
唯一真正需要做的是将名为“[email protected]”的启动图像添加到应用程序资源中,并且在一般情况下(如果您足够幸运)应用程序将正常运行。
In case the app does not handle touch events, then make sure that the key window has the proper size. The workaround is to set the proper frame:
如果应用程序不处理触摸事件,请确保键窗口具有适当的大小。解决方法是设置适当的框架:
[window setFrame:[[UIScreen mainScreen] bounds]]
There are other issues not related to screen size when migrating to iOS 6. Read iOS 6.0 Release Notesfor details.
迁移到 iOS 6 时还有其他与屏幕尺寸无关的问题。请阅读iOS 6.0 发行说明了解详细信息。
回答by SomaMan
Sometimes (for pre-storyboard apps), if the layout is going to be sufficiently different, it's worth specifying a different xib according to device (see this question- you'll need to modify the code to deal with iPhone 5) in the viewController init, as no amount of twiddling with autoresizing masks will work if you need different graphics.
有时(对于 pre-storyboard 应用程序),如果布局将有很大不同,则值得在 viewController 中根据设备指定不同的 xib(请参阅此问题- 您需要修改代码以处理 iPhone 5) init,因为如果您需要不同的图形,那么使用自动调整大小蒙版的任何操作都不起作用。
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
NSString *myNibName;
if ([MyDeviceInfoUtility isiPhone5]) myNibName = @"MyNibIP5";
else myNibName = @"MyNib";
if ((self = [super initWithNibName:myNibName bundle:nibBundleOrNil])) {
...
This is useful for apps which are targeting older iOS versions.
这对于针对较旧 iOS 版本的应用程序很有用。
回答by Manni
Here you can find a nice tutorial (for MonoTouch, but you can use the information for Non-MonoTouch-projects, too):
http://redth.info/get-your-monotouch-apps-ready-for-iphone-5-ios-6-today/
在这里你可以找到一个很好的教程(对于 MonoTouch,但你也可以使用非 MonoTouch 项目的信息):http:
//redth.info/get-your-monotouch-apps-ready-for-iphone-5 -ios-6-今天/
Create a new image for your splash/default screen (640 x 1136 pixel) with the name "[email protected]"
In the iOS Simulator, go to the Hardware -> Device menu, and select "iPhone (Retina 4-inch)"
Create other images, e.g. background images
- Detect iPhone 5 to load your new images:
为您的初始/默认屏幕(640 x 1136 像素)创建一个名为“ [email protected]” 的新图像
在iOS Simulator 中,转到 Hardware -> Device 菜单,然后选择“ iPhone (Retina 4-inch)”
创建其他图像,例如背景图像
- 检测 iPhone 5 以加载您的新图像:
public static bool IsTall
{
get {
return UIDevice.currentDevice.userInterfaceIdiom
== UIUserInterfaceIdiomPhone
&& UIScreen.mainScreen.bounds.size.height
* UIScreen.mainScreen.scale >= 1136;
}
}
private static string tallMagic = "-568h@2x";
public static UIImage FromBundle16x9(string path)
{
//adopt the -568h@2x naming convention
if(IsTall())
{
var imagePath = Path.GetDirectoryName(path.ToString());
var imageFile = Path.GetFileNameWithoutExtension(path.ToString());
var imageExt = Path.GetExtension(path.ToString());
imageFile = imageFile + tallMagic + imageExt;
return UIImage.FromFile(Path.Combine(imagePath,imageFile));
}
else
{
return UIImage.FromBundle(path.ToString());
}
}
回答by shankar
It's easy for migrating iPhone5 and iPhone4 through XIBs.........
通过 XIB 迁移 iPhone5 和 iPhone4 很容易.......
UIViewController *viewController3;
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
UIViewController *viewController3 = [[[mainscreenview alloc] initWithNibName:@"iphone5screen" bundle:nil] autorelease];
}
else
{
UIViewController *viewController3 = [[[mainscreenview alloc] initWithNibName:@"iphone4screen" bundle:nil] autorelease];
}
回答by Shimanski Artem
回答by SkeletonJelly
I had added the new default launch image and (in checking out several other SE answers...) made sure my storyboards all auto-sized themselves and subviews but the retina 4 inches still letterboxed.
我添加了新的默认启动图像,并且(在查看其他几个 SE 答案时...)确保我的故事板都自动调整大小和子视图,但视网膜 4 英寸仍然是信箱。
Then I noticed that my info plist had a line item for "Launch image" set to "Default.png", which I thusly removed and magically letterboxing no longer appeared. Hopefully, that saves someone else the same craziness I endured.
然后我注意到我的信息 plist 有一个“启动图像”的行项设置为“ Default.png”,因此我删除了它并且不再出现神奇的信箱。希望这能让其他人像我所忍受的一样疯狂。
回答by Mike
To determine if your app can support iPhone 5 Retina use this: (This could be more robust to return the type of display, 4S Retina, etc., but as it is written below, it just returns if the iPhone supports iOS5 Retina as a YES or NO)
要确定您的应用程序是否可以支持 iPhone 5 Retina,请使用:(返回显示类型、4S Retina 等可能更健壮,但正如下面所写的,如果 iPhone 支持 iOS5 Retina 作为一个是还是不是)
In a common ".h" file add:
在常见的“.h”文件中添加:
BOOL IS_IPHONE5_RETINA(void);
In a common ".m" file add:
在常见的“.m”文件中添加:
BOOL IS_IPHONE5_RETINA(void) {
BOOL isiPhone5Retina = NO;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if ([UIScreen mainScreen].scale == 2.0f) {
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
if(result.height == 960){
//NSLog(@"iPhone 4, 4s Retina Resolution");
}
if(result.height == 1136){
//NSLog(@"iPhone 5 Resolution");
isiPhone5Retina = YES;
}
} else {
//NSLog(@"iPhone Standard Resolution");
}
}
return isiPhone5Retina;
}
回答by iutinvg
I guess, it is not going to work in all cases, but in my particular project it avoided me from duplication of NIB-files:
我想,它不会在所有情况下都有效,但在我的特定项目中,它避免了我重复 NIB 文件:
Somewhere in common.h
you can make these defines based off of screen height:
common.h
您可以在某个地方根据屏幕高度进行这些定义:
#define HEIGHT_IPHONE_5 568
#define IS_IPHONE ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 ([[UIScreen mainScreen] bounds ].size.height == HEIGHT_IPHONE_5)
In your base controller:
在您的基本控制器中:
- (void)viewDidLoad
{
[super viewDidLoad];
if (IS_IPHONE_5) {
CGRect r = self.view.frame;
r.size.height = HEIGHT_IPHONE_5 - 20;
self.view.frame = r;
}
// now the view is stretched properly and not pushed to the bottom
// it is pushed to the top instead...
// other code goes here...
}