1

I've been trying to determine which segue to use (depending on the current device) using adaptive size classes rather than to use userInterfaceIdiom in code. I saw this mentioned here but don't understand how to do it.

Community
  • 1
  • 1
MickeDG
  • 404
  • 5
  • 18
  • What don't you understand, and what are you trying to do? This question isn't very clear. – jrturton Apr 30 '15 at 08:09
  • If its an iPhone I want to use a show segue (previously push) and if it's an iPad i want to present in modally in a form sheet. I can do this in code like so: `if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { [self presentViewController:vc animated:YES completion:nil]; } else { [vc setPreferredContentSize:CGSizeMake(340, 560)]; [vc setModalPresentationStyle:UIModalPresentationFormSheet]; [self presentViewController:vc animated:YES completion:nil]; } ` – MickeDG Apr 30 '15 at 08:23
  • I'll try to be clearer - from the mention in the link above I get the feeling that it is possible to automatically (without code) use a segue (of several) based on what the current device is. I'm trying to understand if this is the case and if so how to do it. – MickeDG Apr 30 '15 at 08:38

2 Answers2

1

I don't believe it's possible to do this without code as of Xcode 6.3. However to use adaptive size classes rather than user interface idioms, your comment above could be written like this:

if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
    [self presentViewController:vc animated:YES completion:nil];
} else {
    [vc setPreferredContentSize:CGSizeMake(340, 560)];
    [vc setModalPresentationStyle:UIModalPresentationFormSheet];
    [self presentViewController:vc animated:YES completion:nil];
}

This will push for every iPhone except an iPhone 6 Plus in landscape orientation.

Alternatively if you only wanted the modal on iPad (not iPhone 6 Plus) you could do this:

if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular && self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular) {
    [vc setPreferredContentSize:CGSizeMake(340, 560)];
    [vc setModalPresentationStyle:UIModalPresentationFormSheet];
    [self presentViewController:vc animated:YES completion:nil];
} else {
    [self presentViewController:vc animated:YES completion:nil];
}
SeanR
  • 7,899
  • 6
  • 27
  • 38
0

A "Present Modally" segue with the type set to Form Sheet will present the view controller full screen on the iPhone, and use Form Sheet on an iPad. "Show" will push if a navigation controller is present, but I don't think you can choose between Push and Present Modally based on size classes - they're not really equivalent actions.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Yes - I understand and have tried that successfully. But it is still modally on the iPhone - I'd like it to be pushed on the iPhone. I assume that this requires two segues, a Show segue and a Present Modally segue. And I'm wondering if there is an automatic way to select the correct segue based on Size Classes. PS. Like your blog! – MickeDG Apr 30 '15 at 09:38
  • 1
    Your code in your comment presented both times, so that's what I did in my answer :) . I don't think you can choose between a push and a modal presentation based on size classes alone. And thanks! – jrturton Apr 30 '15 at 09:40