繁体   English   中英

如果(设备 == iPad),如果(设备 == iPhone)

[英]if (device == iPad), if (device == iPhone)

所以我有一个通用应用程序,我正在设置UIScrollView的内容大小。 显然,iPhone 和 iPad 上的内容大小会有所不同。 如何为 iPad 设置特定尺寸,为 iPhone 和 iPod touch 设置其他尺寸?

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
     // The device is an iPad running iOS 3.2 or later.
}
else
{
     // The device is an iPhone or iPod touch.
}
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

宏 UI_USER_INTERFACE_IDIOM() 也适用于较旧的 iOS 版本,例如 iOS 3.0,而不会崩溃。

UI_USER_INTERFACE_IDIOM()是您的最佳解决方案,因为您的应用程序是通用的。 但是,如果您在 iPad 上运行 iPhone 应用程序,那么UI_USER_INTERFACE_IDIOM()将返回UIUserInterfaceIdiomPhone ,无论设备如何。 为此,您可以使用UIDevice.model属性:

if ([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound) {

    //Device is iPad
}

在 Swift 2.x 中,您可以使用以下等式来确定设备类型:

UIDevice.currentDevice().userInterfaceIdiom ==.Phone

或者

UIDevice.currentDevice().userInterfaceIdiom ==.Pad

在 Swift 3 中,新人来到这里。

if UIDevice.current.userInterfaceIdiom ==.pad { \\ Available Idioms -.pad, .phone, .tv, .carPlay, .unspecified \\ Implement your awesome logic here }

UIDevice class 将告诉您有关该设备的所有信息。 例如, model属性将告诉您设备的 model。 您可以使用它来确定当前设备使用哪个视图。

使用UIScreen class 确定应用程序的框架。

CGRect usableSpace = [[UIScreen mainScreen] applicationFrame];

返回的矩形是屏幕大小减去状态栏。 不要使用 UI 习惯用法来确定可用空间,因为它们不是面向未来的。

顺便说一句, UIViewController可以自动调整内容视图(包括滚动视图)的大小,并为您提供其他好处,例如自动旋转。 如果它适合您的情况,请考虑它。

如果您使用的是 swift,

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad)
    {
     // device is ipad
    }
    else
    {
     //device is  iPhone 
    }

您还可以检查 UIUSerInterfaceIdiom 并选择您想要的设备 UIUserInterfaceIdiom.Pad 或 UIUserInterfaceIdiom.Phone 或 UIUserInterfaceIdiom.TV 或 UIUserInterfaceIdiom.CarPlay

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM