【问题标题】:How to detect iPhone 6 or iPhone 6 Plus view size mode programmatically如何以编程方式检测 iPhone 6 或 iPhone 6 Plus 视图大小模式
【发布时间】:2016-04-09 10:35:16
【问题描述】:

我想检测 iOS 设备的屏幕尺寸并根据它加载不同的故事板。知道如何在 Swift 中实现这一点。

有没有像

这样的默认属性
if #available(iOS 9.0, *) {
 // load zoomed storyboard 
}else{
 // load standard storyboard 
}

【问题讨论】:

  • 您的链接无效。
  • 你检查过 var device = UIDevice.currentDevice().model
  • 看看stackoverflow.com/questions/30275403/…,应该很容易翻译成Swift。
  • @MartinR 我链接的相同 url 我尝试转换为 swift 但它不起作用你能帮我解决这个问题吗?
  • @NitinGohel UIDevice.currentDevice().model 它返回 iphone 我需要检测 iPhone 的缩放或标准模式

标签: ios iphone swift


【解决方案1】:

试试下面的。您可以轻松检查正在使用的 iDevice。您甚至可以判断应用程序是否在模拟器上运行。

private let DeviceList = [
/* iPod 5 */          "iPod5,1": "iPod Touch 5",
/* iPhone 4 */        "iPhone3,1":  "iPhone 4", "iPhone3,2": "iPhone 4", "iPhone3,3": "iPhone 4",
/* iPhone 4S */       "iPhone4,1": "iPhone 4S",
/* iPhone 5 */        "iPhone5,1": "iPhone 5", "iPhone5,2": "iPhone 5",
/* iPhone 5C */       "iPhone5,3": "iPhone 5C", "iPhone5,4": "iPhone 5C",
/* iPhone 5S */       "iPhone6,1": "iPhone 5S", "iPhone6,2": "iPhone 5S",
/* iPhone 6 */        "iPhone7,2": "iPhone 6",
/* iPhone 6 Plus */   "iPhone7,1": "iPhone 6 Plus",
/* iPhone 6S */       "iPhone8,1": "iPhone 6S",
/* iPhone 6S Plus */  "iPhone8,2": "iPhone 6S Plus",
/* iPad 2 */          "iPad2,1": "iPad 2", "iPad2,2": "iPad 2", "iPad2,3": "iPad 2", "iPad2,4": "iPad 2",
/* iPad 3 */          "iPad3,1": "iPad 3", "iPad3,2": "iPad 3", "iPad3,3": "iPad 3",
/* iPad 4 */          "iPad3,4": "iPad 4", "iPad3,5": "iPad 4", "iPad3,6": "iPad 4",
/* iPad Air */        "iPad4,1": "iPad Air", "iPad4,2": "iPad Air", "iPad4,3": "iPad Air",
/* iPad Air 2 */      "iPad5,1": "iPad Air 2", "iPad5,3": "iPad Air 2", "iPad5,4": "iPad Air 2",
/* iPad Mini */       "iPad2,5": "iPad Mini", "iPad2,6": "iPad Mini", "iPad2,7": "iPad Mini",
/* iPad Mini 2 */     "iPad4,4": "iPad Mini", "iPad4,5": "iPad Mini", "iPad4,6": "iPad Mini",
/* iPad Mini 3 */     "iPad4,7": "iPad Mini", "iPad4,8": "iPad Mini", "iPad4,9": "iPad Mini",
/* Simulator */       "x86_64": "Simulator", "i386": "Simulator"
]


public extension UIDevice {

static var modelName: String {
    var systemInfo = utsname()
    uname(&systemInfo)

    let machine = systemInfo.machine
    let mirror = Mirror(reflecting: machine)

    var identifier = ""

    for child in mirror.children {
        if let value = child.value as? Int8 where value != 0 {
            identifier.append(UnicodeScalar(UInt8(value)))
        }
    }
    return DeviceList[identifier] ?? identifier
}

static var isIphone4: Bool {
    return modelName == "iPhone 5" || modelName == "iPhone 5C" || modelName == "iPhone 5S" || UIDevice.isSimulatorIPhone4
}

static var isIphone5: Bool {
    return modelName == "iPhone 4S" || modelName == "iPhone 4" || UIDevice.isSimulatorIPhone5
}

static var isIphone6: Bool {
    return modelName == "iPhone 6" || UIDevice.isSimulatorIPhone6
}
static var isIphone6Plus: Bool {
    return modelName == "iPhone 6 Plus" || UIDevice.isSimulatorIPhone6Plus
}
static var isIphone6S: Bool {
    return modelName == "iPhone 6S"
}
static var isIphone6SPlus: Bool {
    return modelName == "iPhone 6S Plus"
}


static var isIpad: Bool {
    if (UIDevice.currentDevice().model.rangeOfString("iPad") != nil) {
        return true
    }
    return false
}

static var isIphone: Bool {
    return !self.isIpad
}

/// Check if current device is iPhone4S (and earlier) relying on screen heigth
static var isSimulatorIPhone4: Bool {
    return UIDevice.isSimulatorWithScreenHeigth(480)
}

/// Check if current device is iPhone5 relying on screen heigth
static var isSimulatorIPhone5: Bool {
    return UIDevice.isSimulatorWithScreenHeigth(568)
}

/// Check if current device is iPhone6 relying on screen heigth
static var isSimulatorIPhone6: Bool {
    return UIDevice.isSimulatorWithScreenHeigth(667)
}

/// Check if current device is iPhone6 Plus relying on screen heigth
static var isSimulatorIPhone6Plus: Bool {
    return UIDevice.isSimulatorWithScreenHeigth(736)
}

private static func isSimulatorWithScreenHeigth(heigth: CGFloat) -> Bool {
    let screenSize: CGRect = UIScreen.mainScreen().bounds
    return modelName == "Simulator" && screenSize.height == heigth
}

}

【讨论】:

    【解决方案2】:

    你可以检查nativeScale如果它返回3.0是iPhone 6s,否则返回2.0

    if UIScreen.mainScreen().nativeScale == 3.0 {
       //iphone 6 plus
    } else {
       // iphone 6
    }
    

    正如苹果文档所说:

    物理屏幕的原生比例因子。 (只读)

    适用于 iOS 8.0 及更高版本。

    更新 试试这样的:

    let IS_OS_8_OR_LATER = Float(UIDevice.currentDevice().systemVersion) >= 8.0
    let IS_IPHONE = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone
    let IS_STANDARD_IPHONE_6 = (IS_IPHONE && UIScreen.mainScreen().bounds.size.height == 667.0 && IS_OS_8_OR_LATER && UIScreen.mainScreen().nativeScale == UIScreen.mainScreen().scale)
    let IS_ZOOMED_IPHONE_6 = (IS_IPHONE && UIScreen.mainScreen().bounds.size.height == 568.0 && IS_OS_8_OR_LATER && UIScreen.mainScreen().nativeScale > UIScreen.mainScreen().scale)
    let IS_STANDARD_IPHONE_6_PLUS = (IS_IPHONE && UIScreen.mainScreen().bounds.size.height == 736.0 )
    let IS_ZOOMED_IPHONE_6_PLUS = (IS_IPHONE && UIScreen.mainScreen().bounds.size.height == 667.0  && IS_OS_8_OR_LATER && UIScreen.mainScreen().nativeScale < UIScreen.mainScreen().scale)
    
        if IS_ZOOMED_IPHONE_6_PLUS {
            //do something
        }
    

    我认为您可以对 6sPLUS 和 6PLUS 使用相同的值。

    希望对你有帮助

    【讨论】:

    • 感谢@k8mil 我检查了 iphone 6splus 标准(nativescale==2.60)和 iphone 6splus zoomed(nativescale==2.88)对于其他设备的任何想法说 6,6s,6plus。
    【解决方案3】:

    我建议使用现有的、现成的解决方案来解决此类一般问题。例如,我喜欢https://github.com/erichoracek/UIDevice-Hardware。它是 UIDevice 上的一个类别,可让您轻松确定有关当前设备的很多事情。 (因为它在目标 c 中,你必须通过桥接头来集成它)。

    【讨论】:

      【解决方案4】:

      试试 UIScreen.mainScreen().currentMode。试验它在不同设备上返回的值,然后按照 Vakas 所说的去做。

      【讨论】:

        【解决方案5】:

        这适用于我在 iphone 6+ 和 6s+ 中测试

        if #available(iOS 8.0, *) {
                        print("ios greater than 8")
                        if(UIScreen.mainScreen().bounds.size.height == 667.0 && UIScreen.mainScreen().nativeScale < UIScreen.mainScreen().scale){
        //                    iphone 6+ and 6s+ are in zoomed
                            print("iphone 6+ and 6s+ zoomed ")
                        }else{
                            //                    iphone 6+ and 6s+ are in standard 
                            print("iphone 6+ and 6s+ standard ")
                        }
                    }else{
                        print("ios less than 8")
                    }
        

        感谢@k8mil

        【讨论】:

          猜你喜欢
          • 2015-07-28
          • 1970-01-01
          • 2015-06-05
          • 2014-11-04
          • 2014-11-15
          • 1970-01-01
          • 2016-02-02
          相关资源
          最近更新 更多