2016-04-09 77 views
2

我想檢測iOS設備的屏幕大小並基於該加載不同的故事板。任何想法如何在Swift中實現此目的。如何以編程方式檢測iPhone 6或iPhone 6 Plus查看大小模式

有沒有默認屬性,如

if #available(iOS 9.0, *) { 
// load zoomed storyboard 
}else{ 
// load standard storyboard 
} 
+0

您的鏈接無法正常工作。 –

+0

你檢查過var device = UIDevice.currentDevice()。model –

+0

看看http://stackoverflow.com/questions/30275403/how-to-detect-iphone-6-6-plus-view-mode-以編程方式,應該很容易翻譯成Swift。 –

回答

-1

這對我的作品在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

2

您可以檢查nativeScale如果返回3.0是iPhone 6S否則返回2.0

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

蘋果的文件說:

的物理屏幕的本地比例因子。 (只讀)

適用於iOS 8.0或更高版本。

UPDATE 嘗試是這樣的:

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相同的值。

希望它能幫到你

+0

謝謝@ k8mil我檢查了iPhone 6splus標準(nativescale == 2.60)和iPhone 6splus放大(nativescale == 2.88)任何想法爲其他設備說6,6s,6plus。 –

+0

檢查更新 – kamwysoc

2

請嘗試以下操作。您可以輕鬆地檢查正在使用哪個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 
} 

} 
0

我會建議使用現有的現成的解決方案來處理這樣的通用問題。例如,我喜歡https://github.com/erichoracek/UIDevice-Hardware。它是UIDevice上的一個類別,可以讓您輕鬆確定關於當前設備的很多事情。 (因爲它是在目標c中,你將不得不通過橋接頭來集成它)。

0

嘗試使用UIScreen.mainScreen()。currentMode。嘗試在不同設備上返回的值,並做Vakas所說的。

相關問題