Ios traitCollection.userInterfaceIdiom返回-1
我想检查iPhone或iPad是否使用这些代码Ios traitCollection.userInterfaceIdiom返回-1,ios,xcode,ios8,autolayout,Ios,Xcode,Ios8,Autolayout,我想检查iPhone或iPad是否使用这些代码 if traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.Phone { println("iPhone") } else { println("iPad") } 我试过用iPhone模拟器和我的iPhone,但它总是打印“iPad” 然后我试着打印“traitCollection.userInterfaceIdiom” 它打印“-1”,而不是“0”(iPhone)
if traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.Phone {
println("iPhone")
} else {
println("iPad")
}
我试过用iPhone模拟器和我的iPhone,但它总是打印“iPad”
然后我试着打印“traitCollection.userInterfaceIdiom”
它打印“-1”,而不是“0”(iPhone)或“1”(iPad)
我不知道问题出在哪里,我一直在谷歌上搜索,但没有找到这里发生了什么。你做得对。我将您的代码直接复制并粘贴到应用程序中,与您的代码完全相同:
if traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.Phone {
println("iPhone")
} else {
println("iPad")
}
在iPhone模拟器中,我在控制台中看到iPhone
。在iPad模拟器中,我在控制台中看到iPad
您可能只是在错误的时间运行此代码。请记住,除非您的视图位于界面中,否则不会设置您的trait集合。如果您想在其他时间检查,请询问
UIDevice.currentDevice()
,而不是trait集合,了解userInterfaceIdiom
您做得对。我将您的代码直接复制并粘贴到应用程序中,与您的代码完全相同:
if traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.Phone {
println("iPhone")
} else {
println("iPad")
}
在iPhone模拟器中,我在控制台中看到iPhone
。在iPad模拟器中,我在控制台中看到iPad
您可能只是在错误的时间运行此代码。请记住,除非您的视图位于界面中,否则不会设置您的trait集合。如果您想在其他时间检查,请询问
UIDevice.currentDevice()
,而不是trait集合,了解userInterfaceIdiom
在视图出现之前,trait集合属性是未知的
来自苹果的:
UIUserInterfaceIDIOM未指定
当对象具有特征集合但尚未在环境中时使用。例如,已创建但未放入视图层次结构的视图。在视图出现之前,特征集合属性是未知的 来自苹果的:
UIUserInterfaceIDIOM未指定