什么是等价的UI_USER_INTERFACE_IDIOM()在Swift检测之间的iPhone和iPad?

我得到一个使用未解决的标识符错误时,在Swift编译。


当前回答

Swift 2.0 & iOS 9 & Xcode 7.1

// 1. request an UITraitCollection instance
let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom

// 2. check the idiom
switch (deviceIdiom) {

case .Pad:
    print("iPad style UI")
case .Phone:
    print("iPhone and iPod touch style UI")
case .TV: 
    print("tvOS style UI")
default:
    print("Unspecified UI idiom")

}

Swift 3.0和Swift 4.0

// 1. request an UITraitCollection instance
let deviceIdiom = UIScreen.main.traitCollection.userInterfaceIdiom

// 2. check the idiom
switch (deviceIdiom) {

case .pad:
    print("iPad style UI")
case .phone:
    print("iPhone and iPod touch style UI")
case .tv: 
    print("tvOS style UI")
default:
    print("Unspecified UI idiom")
}

使用UITraitCollection。 iOS trait环境是通过UITraitEnvironment协议的traitCollection属性公开的。以下类采用此协议:

UIScreen ui窗口 ui UIPresentationController UIView

其他回答

从iOS 13开始,UI_USER_INTERFACE_IDIOM已经弃用。如果你的代码仍然是Obj-C,你可以使用以下代码:

if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    // device is iPad
}

地点:

typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) {
    UIUserInterfaceIdiomUnspecified = -1,
    UIUserInterfaceIdiomPhone API_AVAILABLE(ios(3.2)), // iPhone and iPod touch style UI
    UIUserInterfaceIdiomPad API_AVAILABLE(ios(3.2)), // iPad style UI
    UIUserInterfaceIdiomTV API_AVAILABLE(ios(9.0)), // Apple TV style UI
    UIUserInterfaceIdiomCarPlay API_AVAILABLE(ios(9.0)), // CarPlay style UI
};

供参考,我已经使用UI_USER_INTERFACE_IDIOM()为我的应用程序写在Swift。应用程序可以很好地使用XCode 6.3.1编译,没有任何警告,在模拟器(任何选择的设备)和我所有的真实设备(iPhone, iPad)上运行良好,iOS版本从7.1到8.3。

然而,这款应用在苹果评测者的设备上崩溃了(并被拒绝)。我花了几天时间才发现问题,并重新上传了几次到iTunes Connect。

现在我使用UIDevice.currentDevice()。取而代之的是userInterfaceIdiom,我的应用程序可以从这样的崩溃中幸存下来。

对上面的答案做了一些补充,以便返回一个类型而不是字符串值。

我认为这主要是用于UI调整,所以我不认为包括所有的子型号,如iPhone 5s,但这可以很容易地通过添加模型测试扩展到isDevice数组

在Swift 3.1 Xcode 8.3.2中使用物理和模拟器设备进行了测试

实现:

UIDevice.whichDevice ()

public enum SVNDevice {
  case isiPhone4, isIphone5, isIphone6or7, isIphone6por7p, isIphone, isIpad, isIpadPro
}

extension UIDevice {
  class func whichDevice() -> SVNDevice? {
    let isDevice = { (comparision: Array<(Bool, SVNDevice)>) -> SVNDevice? in
      var device: SVNDevice?
      comparision.forEach({
        device = $0.0 ? $0.1 : device
      })
      return device
    }

    return isDevice([
      (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0, SVNDevice.isiPhone4),
      (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0, SVNDevice.isIphone5),
      (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0, SVNDevice.isIphone6or7),
      (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0, SVNDevice.isIphone6por7p),
      (UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0, SVNDevice.isIpad),
      (UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0, SVNDevice.isIpadPro)])
  }
}



private struct ScreenSize {
  static let SCREEN_WIDTH         = UIScreen.main.bounds.size.width
  static let SCREEN_HEIGHT        = UIScreen.main.bounds.size.height
  static let SCREEN_MAX_LENGTH    = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
  static let SCREEN_MIN_LENGTH    = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}

我创建了一个名为SVNBootstaper的框架,其中包括这个和其他一些辅助协议,它是公共的,可以通过Carthage使用。

你可以在Swift 5上使用新的方式:

switch traitCollection.userInterfaceIdiom {
        
    case .unspecified:
        // do something
    case .phone:
        // do something
    case .pad:
        // do something
    case .tv:
        // do something
    case .carPlay:
        // do something
    case .mac:
        // do something
    @unknown default:
        // do something
}

Swift 2.0 & iOS 7+ / iOS 8+ / iOS 9+

public class Helper {
    public class var isIpad:Bool {
        if #available(iOS 8.0, *) {
            return UIScreen.mainScreen().traitCollection.userInterfaceIdiom == .Pad
        } else {
            return UIDevice.currentDevice().userInterfaceIdiom == .Pad
        }
    }
    public class var isIphone:Bool {
        if #available(iOS 8.0, *) {
            return UIScreen.mainScreen().traitCollection.userInterfaceIdiom == .Phone
        } else {
            return UIDevice.currentDevice().userInterfaceIdiom == .Phone
        }
    }
}

使用:

if Helper.isIpad {

}

OR

guard Helper.isIpad else {
    return
} 

由于@user3378170