如何在iphone和ipad的通用应用程序中调用[[UIScreen mainScreen] scale] [英] How to call [[UIScreen mainScreen] scale] in a universal app for the iphone and ipad

查看:82
本文介绍了如何在iphone和ipad的通用应用程序中调用[[UIScreen mainScreen] scale]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一款可在ipad和iphone上运行的通用应用。到目前为止一切都那么好,但我刚刚将我的SDK更新为ios4并希望调用[[UIScreen mainScreen] scale](缩放不在3.2 sdk中,而ipad还没有ios4)。

I'm making a universal app that will run on both the ipad and the iphone. So far so good, but I have just updated my SDK to ios4 and am wanting to call [[UIScreen mainScreen] scale] (scale is not in the 3.2 sdk and the ipad doesn't have ios4 yet).

我知道我可以调用[[UIScreen mainScreen] respondsToSelector:@selector(scale)]来查明我是否可以调用它,但我仍然需要我的代码中的函数调用(并且能够访问返回值),以便它可以在iphone上运行。

I know that I can call [[UIScreen mainScreen] respondsToSelector:@selector(scale)] to find out if I can call it, but I still need to have the function call (and be able to access the return value) in my code so that it can run on the iphone.

编辑:为了清楚起见,我的问题是在构建3.2 SDK时使用代码[[UIScreen mainScreen] scale]时出错。此错误是分配中的不兼容类型。所以我需要能够仍然为4.0 SDK调用此函数,但仍然有3.2 SDK的项目构建。

EDITED: To be clear, my problem is that there is an error when using the code [[UIScreen mainScreen] scale] when building for 3.2 SDK. This error is "Incompatible types in assignment". So I need to be able to still call this function for 4.0 SDK but still have the project build for 3.2 SDK.

推荐答案

,你可以在if语句中包含每个调用,如下所示:

Well, you could just wrap every call to it in an if statement, like so:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    //do scale stuff here
}



<但是,更好的方法(可能需要重组整个应用程序)是为iPad和iPhone设置单独的视图控制器。

But a better way (which might require restructuring your whole app, though) is to have separate view controllers for iPad and iPhone.

要获得跨平台视图或其他设备的规模,您可以这样做:

To get the scale of the device for a cross platform view or something, you could do like this:

CGFloat scale;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    scale=[[UIScreen mainScreen] scale];
} else {
    scale=1; //only called on iPad.
}

为避免每次都输入此内容,您可以在UIScreen上声明一个类别,在-realScale方法中使用此代码。

To avoid typing this every time, you could declare a category on UIScreen, which uses this code inside a -realScale method or something.

所有这些方法都需要将基本SDK设置为4.0(以便您可以访问4.0 API)和最小值iPhone部署目标为3.2(因此它将在iPad上运行)

All of these methods require setting the base SDK to 4.0 (so that you can access the 4.0 APIs) and the minimum iPhone deployment target to 3.2 (so it will run on iPad)

这篇关于如何在iphone和ipad的通用应用程序中调用[[UIScreen mainScreen] scale]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆