这段代码有什么问题?我想要检查用户当前使用的设备是否是iPad,但它一直给我错误。
if (UIUserInterfaceIdiom == UIUserInterfaceIdiomPad)
{
//do stuff
}
你可以使用
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
在 Swift 中,您可以使用:
if UIDevice.current.userInterfaceIdiom == .pad {
//do stuff
}
您需要检查设备是否为 iPhone/iPad。
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//do ur ipad logic
}else
{
//do ur iphone logic
}
/* UI_USER_INTERFACE_IDIOM()函数适用于部署到iOS 3.2以下版本时使用。如果您将部署的iPhone/iOS的最早版本为3.2或更高版本,则可以直接使用-[UIDevice userInterfaceIdiom]。 */
除非您支持超级老的iOS,否则最好使用UIDevice.current.userInterfaceIdiom
。
可能的情况如下:
public enum UIUserInterfaceIdiom : Int {
case unspecified
@available(iOS 3.2, *)
case phone // iPhone and iPod touch style UI
@available(iOS 3.2, *)
case pad // iPad style UI
@available(iOS 9.0, *)
case tv // Apple TV style UI
@available(iOS 9.0, *)
case carPlay // CarPlay style UI
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad)
{
// device is ipad
}
else
{
// device is iPhone
}
我的应用程序在iPad上作为仅iPhone应用程序运行,但我仍然需要检测设备是否为iPad以隐藏与Apple HealthKit相关的某些功能,因为它不可用于iPad。我使用了这种方法。
extension UIDevice {
var isPad: Bool {
get {
return UIDevice.deviceInfo.lowercased().containsWord("ipados")
}
}
}
OBJC:
if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad){
//do awesome objc stuff!;
}