How can I maintain split terrain views on iPhone 6 or 5?

In my application, there is a lot of missing landscape space on the iPhone 6 and to a lesser extent even on 4-inch screens. I already use the IOS 8 UISplitViewController changes to support dual-pane landscape rendering on the iPhone 6 Plus, but it would be useful to see both panels on some smaller devices.

Conveniently, Apple hosted the WWDC 2014 session, “Creating Responsive Applications Using UIKit,” which detailed how to do this. You can download the sample code here , but in short: they put the UISplitViewController inside a subclass of UIViewController . The subclass uses setOverrideTraitCollection:forChildViewController: to force [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular] in split mode when it considers the width wide enough. At that time, the sample code worked well and still works on most devices.

However, trying to use this code in my own application, I found that it works poorly on the iPhone 6 Plus. You yourself see this if you download the sample code and make two changes:

  • Add a storyboard, add an empty view controller to it, and set it as the “Launch Screen File”. This is necessary to run the application in its own resolution on 6 Plus.
  • In AAPLTraitOverrideViewController.m change line 21 to size.width > 500.0 or to something larger than 414. This is necessary to ensure that only one view in the portrait is viewed in portrait mode on 6 Plus.

Now you can run the application in the simulator. To see the problem, simply do the following:

  • Rotate the device to landscape (right arrow command line)
  • Turn it immediately back to the portrait (left-right arrow)

You can already see that something is wrong. All cells in the table should have an arrow on the right side in the portrait, but they do not. They behave as if they are still in section. If you click one of these lines, it gets worse - the detail view slides from below, and the navigation bar disappears.

I think there should be a bug in iOS 8 that is causing the problem. But since this code was brought before the iPhone 6 Plus was announced, it also seems possible that it just needs some settings to make it compatible with this device. So far, the only solution I've found is to change line 21 to something like if (size.width > 500.0 && size.width < 736.0) , but I don't want to use code that could break again the next time Apple introduces new screen size. Is there a better way to handle this?

+6
source share
1 answer

It looks like you always want to make the horizontal size of the class regular ( UIUserInterfaceSizeClassRegular ). To do this, override traitCollectionDidChange: In this method, if the vertical size class is compact (suppose it is likely in the landscape), redefine the feature collection so that the horizontal size class is regular.

 UITraitCollection *compactHeight = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassCompact]; if ([self.traitCollection containsTraitsInCollection:compactHeight]) { UITraitCollection *regularWidth = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular]; self.forcedTraitCollection = [UITraitCollection traitCollectionWithTraitsFromCollections:@[self.traitCollection, regularWidth]]; [self setOverrideTraitCollection:self.forcedTraitCollection forChildViewController:_viewController]; } else { [self setOverrideTraitCollection:nil forChildViewController:_viewController]; } 

However, if you need more specific behavior, you will have to rely on the size of the canvas for the behavior of the application.

+8
source

Source: https://habr.com/ru/post/975805/


All Articles