我有一款应用可以在iPhone和iPod Touch上运行,也可以在Retina iPad和其他设备上运行,但需要做一些调整。我需要检测当前设备是否是iPad。我可以用什么代码来检测用户是否在我的UIViewController中使用iPad,然后相应地改变一些东西?
当前回答
注意:如果你的应用只针对iPhone设备,在iPhone兼容模式下运行的iPad将为以下语句返回false:
#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
检测实体iPad设备的正确方法是:
#define IS_IPAD_DEVICE ([(NSString *)[UIDevice currentDevice].model hasPrefix:@"iPad"])
其他回答
在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
}
很多答案都很好,但我在swift 4中使用这样的答案
创建常量 struct App { 静态let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ?True: false } 像这样使用 if App.isRunningOnIpad { 返回加载(from: .main, identifier: identifier) }其他{ 返回加载(from: .ipad, identifier: identifier) }
编辑:建议ceure简单地在UIDevice上创建一个扩展
extension UIDevice {
static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false
}
你也可以用这个
#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
...
if (IPAD) {
// iPad
} else {
// iPhone / iPod Touch
}
在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
}
}
有很多方法可以检查设备是否为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
推荐文章
- 我应该如何从字符串中删除所有的前导空格?- - - - - -斯威夫特
- Xcode构建失败“架构x86_64未定义的符号”
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- 新的自动引用计数机制是如何工作的?
- 如何测试对象在Objective-C中的类?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 从Cocoa应用程序执行一个终端命令
- Swift:理解// MARK
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- Swift编译器错误:“框架模块内的非模块化头”
- 从父iOS访问容器视图控制器