我有一款应用可以在iPhone和iPod Touch上运行,也可以在Retina iPad和其他设备上运行,但需要做一些调整。我需要检测当前设备是否是iPad。我可以用什么代码来检测用户是否在我的UIViewController中使用iPad,然后相应地改变一些东西?
当前回答
在Swift中有很多方法可以做到这一点:
我们检查下面的模型(这里我们只能进行区分大小写的搜索):
class func isUserUsingAnIpad() -> Bool {
let deviceModel = UIDevice.currentDevice().model
let result: Bool = NSString(string: deviceModel).containsString("iPad")
return result
}
我们检查下面的模型(我们可以在这里进行区分大小写的搜索):
class func isUserUsingAnIpad() -> Bool {
let deviceModel = UIDevice.currentDevice().model
let deviceModelNumberOfCharacters: Int = count(deviceModel)
if deviceModel.rangeOfString("iPad",
options: NSStringCompareOptions.LiteralSearch,
range: Range<String.Index>(start: deviceModel.startIndex,
end: advance(deviceModel.startIndex, deviceModelNumberOfCharacters)),
locale: nil) != nil {
return true
} else {
return false
}
}
UIDevice.currentDevice()。下面的userInterfaceIdiom只返回iPad,如果应用程序是iPad或Universal。如果这是一款在iPad上运行的iPhone应用,那么它就不会这么做。所以你应该检查模型。:
class func isUserUsingAnIpad() -> Bool {
if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad {
return true
} else {
return false
}
}
如果类没有继承UIViewController,下面的代码段不会编译,否则它就可以正常工作。无论如何,UI_USER_INTERFACE_IDIOM()只返回iPad,如果应用程序是iPad或通用。如果这是一款在iPad上运行的iPhone应用,那么它就不会这么做。所以你应该检查模型。:
class func isUserUsingAnIpad() -> Bool {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad) {
return true
} else {
return false
}
}
其他回答
你可以检查rangeOfString来查看单词iPad是否像这样存在。
NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;
if ([deviceModel rangeOfString:@"iPad"].location != NSNotFound) {
NSLog(@"I am an iPad");
} else {
NSLog(@"I am not an iPad");
}
有很多方法可以检查设备是否为iPad。这是我最喜欢的检查设备是否真的是iPad的方法:
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
return YES; /* Device is iPad */
}
我使用它的方式
#define IDIOM UI_USER_INTERFACE_IDIOM()
#define IPAD UIUserInterfaceIdiomPad
if ( IDIOM == IPAD ) {
/* do something specifically for iPad. */
} else {
/* do something specifically for iPhone or iPod touch. */
}
其他的例子
if ( [(NSString*)[UIDevice currentDevice].model hasPrefix:@"iPad"] ) {
return YES; /* Device is iPad */
}
#define IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
if ( IPAD )
return YES;
有关Swift解决方案,请参阅以下答案:https://stackoverflow.com/a/27517536/2057171
对于最新版本的iOS,只需添加UITraitCollection:
extension UITraitCollection {
var isIpad: Bool {
return horizontalSizeClass == .regular && verticalSizeClass == .regular
}
}
然后在UIViewController中检查:
if traitCollection.isIpad { ... }
在Swift 4.2和Xcode 10中
if UIDevice().userInterfaceIdiom == .phone {
//This is iPhone
} else if UIDevice().userInterfaceIdiom == .pad {
//This is iPad
} else if UIDevice().userInterfaceIdiom == .tv {
//This is Apple TV
}
如果你想检测特定的设备
let screenHeight = UIScreen.main.bounds.size.height
if UIDevice().userInterfaceIdiom == .phone {
if (screenHeight >= 667) {
print("iPhone 6 and later")
} else if (screenHeight == 568) {
print("SE, 5C, 5S")
} else if(screenHeight<=480){
print("4S")
}
} else if UIDevice().userInterfaceIdiom == .pad {
//This is iPad
}
你也可以用这个
#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
...
if (IPAD) {
// iPad
} else {
// iPhone / iPod Touch
}
推荐文章
- 为什么Swift的编译时间这么慢?
- iPhone上UIView和UILabels的渐变
- 如何测试等价的Swift枚举与相关的值
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- 在Swift中转换Float为Int
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 我如何在Swift中声明一个弱引用数组?
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?