如何在扩展程序文件中放置我的代码以识别设备?
if UIDevice().userInterfaceIdiom == .pad {
switch UIScreen.main.nativeBounds.height {
case 2048:
print("iPad Pro 9.7/Air")
case 2224:
print("iPad Pro 10.5")
case 2732:
print("iPad Pro 12.9")
displayResultLabel.frame = CGRect(x: 2, y: 90, width: 370, height: 91)
displayResultLabel.font = displayResultLabel.font.withSize(105)
default:
print("unknown")
}
}
答案 0 :(得分:2)
extension THE_NAME_OF_CLASS_TO_EXTEND {
func printDevice() {
if UIDevice().userInterfaceIdiom == .pad {
switch UIScreen.main.nativeBounds.height {
case 2048:
print("iPad Pro 9.7/Air")
case 2224:
print("iPad Pro 10.5")
case 2732:
print("iPad Pro 12.9")
displayResultLabel.frame = CGRect(x: 2, y: 90, width: 370, height: 91)
displayResultLabel.font = displayResultLabel.font.withSize(105)
default:
print("unknown")
}
}
}
}
然后你可以用这个:
THE_INSTANCE_OF_THE_CLASS_THAT_HAS_THE_NAME_OF_CLASS_TO_EXTEND.printDevice()
所以你只需要将该代码复制到一个新文件中。
将 THE_NAME_OF_CLASS_TO_EXTEND 替换为您班级的名称
并将 THE_INSTANCE_OF_THE_CLASS_THAT_HAS_THE_NAME_OF_CLASS_TO_EXTEND 替换为该类的INSTANCE名称..比它更好地工作=]