我有一个通用应用程序,正在设置UIScrollView
的内容大小。很明显,在iPhone和iPad上,内容大小会有所不同。我该如何为iPad设置特定大小,为iPhone和iPod touch设置另一种大小?
我有一个通用应用程序,正在设置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
}
UIDevice.model
属性等于“设置”>“关于”中显示的字符串,并且可以被用户更改。来源:https://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instp/UIDevice/name - KennethJUIDevice.currentDevice().userInterfaceIdiom == .Phone
或者
UIDevice.currentDevice().userInterfaceIdiom == .Pad
在Swift 3中,对于新手来说,您需要使用以下代码:
if UIDevice.current.userInterfaceIdiom == .pad {
\\ 可用选项 - .pad, .phone, .tv, .carPlay, .unspecified
\\ 在这里实现您的惊人逻辑
}
UIScreen
类来确定应用程序的框架。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