30

So I have a universal app and I'm setting up the content size of a UIScrollView. Obviously the content size is going to be different on iPhones and iPads. How can I set a certain size for iPads and another size for iPhones and iPod touches?

Peter Kazazes
  • 3,600
  • 7
  • 31
  • 60
  • possible duplicate of [API to determine whether running on iPhone or iPad](http://stackoverflow.com/questions/2884391/api-to-determine-whether-running-on-iphone-or-ipad) – Pang Mar 03 '14 at 08:49

7 Answers7

93
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
     // The device is an iPad running iOS 3.2 or later.
}
else
{
     // The device is an iPhone or iPod touch.
}
Deniss Fedotovs
  • 1,384
  • 12
  • 22
Peter Kazazes
  • 3,600
  • 7
  • 31
  • 60
14
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

and

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

The macros UI_USER_INTERFACE_IDIOM() also works on older iOS versions like iOS 3.0 without crashing.

JustSid
  • 25,168
  • 7
  • 79
  • 97
5

UI_USER_INTERFACE_IDIOM() is the best solution in your case since your app is universal. But if you are running an iPhone app on iPad than UI_USER_INTERFACE_IDIOM() will return UIUserInterfaceIdiomPhone, regardless of the device. For such purposes as that, you can use the UIDevice.model property:

if ([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound) {

    //Device is iPad
}
tfrank377
  • 1,858
  • 2
  • 22
  • 34
Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
  • This is not realiable. As far as I'm aware, the `UIDevice.model` property is equal to the string present in Settings > About and is changable by the user. Source: https://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instp/UIDevice/name – KennethJ Sep 02 '14 at 02:55
  • @KennethJ thanks for the warning but I have checked it and I don't think so. Settings > general > about > Model gives "MD297B/A" for my iPhone 5 and is not editable (on the menu only the field "name" is editable). The API link you gave states that device.model is readonly and @”iPhone” and @”iPod touch” are possible values. – Yunus Nedim Mehel Sep 02 '14 at 08:52
  • 1
    Yes you are quite right, I was mistakenly looking at "name" rather than "model". I stand corrected. – KennethJ Sep 03 '14 at 14:56
3

In Swift 2.x you can use the following equalities to determine the kind of device:

UIDevice.currentDevice().userInterfaceIdiom == .Phone

or

UIDevice.currentDevice().userInterfaceIdiom == .Pad

In Swift 3 for new people coming here.

if UIDevice.current.userInterfaceIdiom == .pad { \\ Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified \\ Implement your awesome logic here }

Kunal Verma
  • 562
  • 4
  • 15
2

The UIDevice class will tell you everything you need to know about the device. The model property, for instance, will tell you the model of the device. You can use this to determine which view to use for the current device.

csano
  • 13,266
  • 2
  • 28
  • 45
  • It's not recommended to use the model property as it's not future-proof. – Constantino Tsarouhas Aug 12 '11 at 21:49
  • That's the first I've heard of this. Can you provide me with a link? If this is the case, I'll remove my answer. – csano Aug 12 '11 at 22:07
  • 1
    Actually, I heard it on a WWDC session on making iOS apps future-proof. Using the model property gives you a string. If Apple releases a new iOS device that is similar to the iPhone but has another name, say "iPhone nano", the app won't be able to function correctly. I advise using UIScreen's applicationFrame property for this situation. It's analogous to determining hardware capabilities: instead of only enabling the gyroscope on the iPhone and the iPad, it's better to enable it only if Core Motion says so. If once the iPod touch or a future device also supports it, you've got a problem. ;-) – Constantino Tsarouhas Aug 13 '11 at 00:04
1

Use the UIScreen class to determine the application's frame.

CGRect usableSpace = [[UIScreen mainScreen] applicationFrame];

The returned rectangle is the screen's size minus the status bar. Don't use the UI idioms for determining available space as they're not future-proof.

By the way, UIViewController can resize content views (including scroll views) automatically, as well as provide you with other goodies, such as auto-rotation. Consider it if it's appropriate in your situation.

Constantino Tsarouhas
  • 6,846
  • 6
  • 43
  • 54
  • Idioms are the way they are now, and it's trivial to change it in the future. They're a lot more clear and meaningful than your suggestion, IMHO. – FeifanZ Oct 01 '11 at 00:06
  • 1
    @Inspire48: The question is: how can I properly size my scroll view? The easiest way to do that is by using the size the system gives. So, even on a iDevice not yet known, the code wouldn't break, because you wouldn't care about the device (idioms), but only about the display (sizing a scroll view, etc.). Obviously, for many other circumstances, using the idiom is the way to go. ;-) – Constantino Tsarouhas Oct 02 '11 at 03:41
0

if you are using swift,

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad)
    {
     // device is ipad
    }
    else
    {
     //device is  iPhone 
    }

You can also check the UIUSerInterfaceIdiom and choose the device you want to UIUserInterfaceIdiom.Pad or UIUserInterfaceIdiom.Phone or UIUserInterfaceIdiom.TV or UIUserInterfaceIdiom.CarPlay

Raja Reddy
  • 57
  • 1