Alb*_*haw 248 objective-c device ipad ios swift
我有一个在iPhone和iPod Touch上运行的应用程序,它可以在Retina iPad上运行,但除此之外需要进行一次调整.我需要检测当前设备是否是iPad.我可以使用哪些代码来检测用户是否在我的iPad中使用iPad UIViewController然后相应地更改了某些内容?
Wri*_*sCS 578
有很多方法可以检查设备是否是iPad.这是我最喜欢检查设备是否实际上是iPad的方法:
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
return YES; /* Device is iPad */
}
Run Code Online (Sandbox Code Playgroud)
#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. */
}
Run Code Online (Sandbox Code Playgroud)
if ( [(NSString*)[UIDevice currentDevice].model hasPrefix:@"iPad"] ) {
return YES; /* Device is iPad */
}
#define IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
if ( IPAD )
return YES;
Run Code Online (Sandbox Code Playgroud)
有关Swift解决方案,请参阅以下答案:https://stackoverflow.com/a/27517536/2057171
Jee*_*hut 150
在Swift中,您可以使用以下等式来确定通用应用程序上的设备类型:
UIDevice.current.userInterfaceIdiom == .phone
// or
UIDevice.current.userInterfaceIdiom == .pad
Run Code Online (Sandbox Code Playgroud)
用法将是这样的:
if UIDevice.current.userInterfaceIdiom == .pad {
// Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified
// Implement your logic here
}
Run Code Online (Sandbox Code Playgroud)
Ric*_*ard 33
这是iOS 3.2中UIDevice的一部分,例如:
[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad
Run Code Online (Sandbox Code Playgroud)
小智 25
你也可以用这个
#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
...
if (IPAD) {
// iPad
} else {
// iPhone / iPod Touch
}
Run Code Online (Sandbox Code Playgroud)
mal*_*hal 20
如果应用程序适用于iPad或Universal,UI_USER_INTERFACE_IDIOM()仅返回iPad.如果它的iPhone应用程序在iPad上运行,那么它就不会.所以你应该检查模型.
小智 15
小心:如果您的应用仅针对iPhone设备,则使用iphone兼容模式运行的iPad将在以下声明中返回false:
#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
Run Code Online (Sandbox Code Playgroud)
检测物理iPad设备的正确方法是:
#define IS_IPAD_DEVICE ([(NSString *)[UIDevice currentDevice].model hasPrefix:@"iPad"])
Run Code Online (Sandbox Code Playgroud)
And*_*ies 13
我发现在Xcode中的模拟器中,某些解决方案对我不起作用.相反,这有效:
NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;
if ([[deviceModel substringWithRange:NSMakeRange(0, 4)] isEqualToString:@"iPad"]) {
DebugLog(@"iPad");
} else {
DebugLog(@"iPhone or iPod Touch");
}
Run Code Online (Sandbox Code Playgroud)
if UIDevice.current.model.hasPrefix("iPad") {
print("iPad")
} else {
print("iPhone or iPod Touch")
}
Run Code Online (Sandbox Code Playgroud)
此外,在Xcode的"其他示例"中,设备模型以"iPad模拟器"的形式返回,因此上述调整应该对其进行排序.
在Swift中有很多方法可以做到这一点:
我们检查下面的模型(我们只能在这里进行区分大小写的搜索):
class func isUserUsingAnIpad() -> Bool {
let deviceModel = UIDevice.currentDevice().model
let result: Bool = NSString(string: deviceModel).containsString("iPad")
return result
}
Run Code Online (Sandbox Code Playgroud)
我们检查下面的模型(我们可以在这里做一个区分大小写/不敏感的搜索):
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
}
}
Run Code Online (Sandbox Code Playgroud)
UIDevice.currentDevice().userInterfaceIdiom如果该应用适用于iPad或Universal,则仅返回iPad.如果它是在iPad上运行的iPhone应用程序,那么它不会.所以你应该检查模型.:
class func isUserUsingAnIpad() -> Bool {
if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad {
return true
} else {
return false
}
}
Run Code Online (Sandbox Code Playgroud)
如果该类没有继承a UIViewController,则下面的代码段不会编译,否则它可以正常工作.UI_USER_INTERFACE_IDIOM()如果应用程序适用于iPad或Universal,则无论如何都会返回iPad.如果它是在iPad上运行的iPhone应用程序,那么它不会.所以你应该检查模型.:
class func isUserUsingAnIpad() -> Bool {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad) {
return true
} else {
return false
}
}
Run Code Online (Sandbox Code Playgroud)
*
在swift 3.0中
*
if UIDevice.current.userInterfaceIdiom == .pad {
//pad
} else if UIDevice.current.userInterfaceIdiom == .phone {
//phone
} else if UIDevice.current.userInterfaceIdiom == .tv {
//tv
} else if UIDevice.current.userInterfaceIdiom == .carPlay {
//CarDisplay
} else {
//unspecified
}
Run Code Online (Sandbox Code Playgroud)
许多答案都不错,但我在Swift 4中这样使用
创建常量
struct App {
static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false
}
Run Code Online (Sandbox Code Playgroud)这样使用
if App.isRunningOnIpad {
return load(from: .main, identifier: identifier)
} else {
return load(from: .ipad, identifier: identifier)
}
Run Code Online (Sandbox Code Playgroud)编辑: 按照建议,Cur仅在UIDevice上创建扩展
extension UIDevice {
static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false
}
Run Code Online (Sandbox Code Playgroud)
在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
}
Run Code Online (Sandbox Code Playgroud)
如果你想检测特定设备
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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
142611 次 |
| 最近记录: |