Best way to programmatically detect iPad/iPhone hardware

前端 未结 10 938
借酒劲吻你
借酒劲吻你 2020-11-27 14:14

The reason I need to find out is that on an iPad, a UIPickerView has the same height in landscape orientation as it does in portrait. On an iPhone it is different. The iPad

10条回答
  •  一个人的身影
    2020-11-27 14:48

    I'm answering this now (and at this late date) because many of the existing answers are quite old, and the most Up Voted actually appears to be wrong according to Apples current docs (iOS 8.1, 2015)!

    To prove my point, this is the comment from Apples header file (always look at the Apple source and headers):

    /*The UI_USER_INTERFACE_IDIOM() macro is provided for use when
      deploying to a version of the iOS less than 3.2. If the earliest
      version of iPhone/iOS that you will be deploying for is 3.2 or
      greater, you may use -[UIDevice userInterfaceIdiom] directly.*/
    

    Therefore, the currently APPLE recommended way to detect iPhone vs. iPad, is as follows:

    1) (DEPRECATED as of iOS 13) On versions of iOS PRIOR to 3.2, use the Apple provided macro:

    // for iPhone use UIUserInterfaceIdiomPhone
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    

    2) On versions of iOS 3.2 or later, use the property on [UIDevice currentDevice]:

    // for iPhone use UIUserInterfaceIdiomPhone
    if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
    

提交回复
热议问题