ios - 如何根据iPhone 8、X或iPad的屏幕尺寸更改代码

标签 ios swift xcode swift4

我在另一篇文章中找到了代码。

if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
 {
     if ([[UIScreen mainScreen] bounds].size.height == 568)
     {


     }
     else
     {
         //iphone 3.5 inch screen
     }
 }
 else
 {
        //[ipad]
 }

这篇文章是5年前的文章代码。 我认为它太旧了。 因为它不兼容iPhone 10。

请告诉我允许灵 active 的最新代码。

最佳答案

这就是我在我的应用程序中使用的;这是一种享受:

if UIDevice().userInterfaceIdiom == .phone && UIScreen.main.nativeBounds.height == 2436 {
   // Device is iPhone X 
}else if UIDevice().userInterfaceIdiom == .pad && UIScreen.main.nativeBounds.height == 1668 {
   // Device is iPad Pro 10.5' X 
}else{ 
   //Device is something else 
}

如果您转到Apple Developer Documentation Archive ,您可以选择要用于检测特定设备的属性。如果存在多种情况,也许 switch 语句会更合适。

关于ios - 如何根据iPhone 8、X或iPad的屏幕尺寸更改代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51131020/

相关文章:

xcode - 我什么时候可以使用 "Prefer margin relative"?

ios - CALayer 自动布局

ios - 使用 Google SignIn 登录后分享到 Google+

ios - 如何使方形 ImageView 自动布局

ios - 覆盖 UIViewConrtoller 扩展中的 PreferredStatusBarStyle

arrays - 为什么在 swift4 中有一个数组为空和错误?

ios - 为 iphone 3gs/4/4s/5/6/6plus 应用程序选择像素完美背景图像

xcode - 有没有办法简化键入 Swift 尾随闭包?

ios - 'UINavigationBar'的可见@interface没有声明选择器'titleTextAttributes:'

ios - 每个目标不同的 Storyboard本地化