ios 如果(设备 == iPad),如果(设备 == iPhone)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7046539/
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
if (device == iPad), if (device == iPhone)
提问by Peter Kazazes
So I have a universal app and I'm setting up the content size of a UIScrollView
. Obviously the content size is going to be different on iPhones and iPads. How can I set a certain size for iPads and another size for iPhones and iPod touches?
所以我有一个通用应用程序,我正在设置UIScrollView
. 显然,iPhone 和 iPad 上的内容大小会有所不同。如何为 iPad 设置特定尺寸,为 iPhone 和 iPod touch 设置另一个尺寸?
回答by Peter Kazazes
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.
}
回答by JustSid
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
and
和
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
The macros UI_USER_INTERFACE_IDIOM() also works on older iOS versions like iOS 3.0 without crashing.
宏 UI_USER_INTERFACE_IDIOM() 也适用于较旧的 iOS 版本,如 iOS 3.0,而不会崩溃。
回答by Yunus Nedim Mehel
UI_USER_INTERFACE_IDIOM()
is the best solution in your case since your app is universal. But if you are running an iPhone app on iPad than UI_USER_INTERFACE_IDIOM()
will return UIUserInterfaceIdiomPhone
, regardless of the device. For such purposes as that, you can use the UIDevice.model
property:
UI_USER_INTERFACE_IDIOM()
是您的最佳解决方案,因为您的应用程序是通用的。但是,如果您在 iPad 上运行 iPhone 应用程序,那么无论设备如何,UI_USER_INTERFACE_IDIOM()
都会返回UIUserInterfaceIdiomPhone
。为此,您可以使用该UIDevice.model
属性:
if ([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound) {
//Device is iPad
}
回答by Kunal Verma
In Swift 2.x you can use the following equalities to determine the kind of device:
在 Swift 2.x 中,您可以使用以下等式来确定设备类型:
UIDevice.currentDevice().userInterfaceIdiom == .Phone
UIDevice.currentDevice().userInterfaceIdiom == .Phone
or
或者
UIDevice.currentDevice().userInterfaceIdiom == .Pad
UIDevice.currentDevice().userInterfaceIdiom == .Pad
In Swift 3 for new people coming here.
在 Swift 3 中,新人来到这里。
if UIDevice.current.userInterfaceIdiom == .pad {
\\ Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified
\\ Implement your awesome logic here
}
if UIDevice.current.userInterfaceIdiom == .pad {
\\ Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified
\\ Implement your awesome logic here
}
回答by csano
回答by Constantino Tsarouhas
Use the UIScreen
class to determine the application's frame.
使用UIScreen
该类来确定应用程序的框架。
CGRect usableSpace = [[UIScreen mainScreen] applicationFrame];
The returned rectangle is the screen's size minus the status bar. Don't use the UI idioms for determining available space as they're not future-proof.
返回的矩形是屏幕大小减去状态栏。不要使用 UI 习惯用法来确定可用空间,因为它们不是面向未来的。
By the way, UIViewController
can resize content views (including scroll views) automatically, as well as provide you with other goodies, such as auto-rotation. Consider it if it's appropriate in your situation.
顺便说一句,UIViewController
可以自动调整内容视图(包括滚动视图)的大小,并为您提供其他好处,例如自动旋转。如果它适合您的情况,请考虑它。
回答by Raja Reddy
if you are using swift,
如果您使用的是 swift,
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad)
{
// device is ipad
}
else
{
//device is iPhone
}
You can also check the UIUSerInterfaceIdiom and choose the device you want to UIUserInterfaceIdiom.Pad or UIUserInterfaceIdiom.Phone or UIUserInterfaceIdiom.TV or UIUserInterfaceIdiom.CarPlay
您还可以检查 UIUSerInterfaceIdiom 并选择您想要的设备 UIUserInterfaceIdiom.Pad 或 UIUserInterfaceIdiom.Phone 或 UIUserInterfaceIdiom.TV 或 UIUserInterfaceIdiom.CarPlay