UISplitViewController doesn't work on iPhone(5 nor 6)

家住魔仙堡 提交于 2020-01-06 18:06:30

问题


On iPad, I have perfectly working UISplitViewController. I can hide and show its primaryViewController, and splitViewController:willChangeToDisplayMode: is called in appropriate way.

But on iPhone, something is wrong.
I can show primaryViewController, but cannot hide it, because the primaryViewController appears in full screen size. It's so full that I can't touch the secondary view, in that way I can hide the primaryViewController on iPad.
splitViewController:willChangeToDisplayMode: is not called either.

I have a viewDidLoad below, in my custom UISplitViewController class.

// UISplitViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    self.preferredPrimaryColumnWidthFraction = .1;
    CGRect mainScreen = [[UIScreen mainScreen] bounds];
    self.minimumPrimaryColumnWidth = 270;
    self.maximumPrimaryColumnWidth = mainScreen.size.width - 5;
}

On iPhone, any of these property seems not to be working: preferredPrimaryColumnWidthFraction or minimum/maximumPrimaryColumnWidth

I am adding this splitViewController as rootViewController in AppDelegate.m by the code below.

// AppDelegate.m
[_splitViewCon addChildViewController: tagNaviCon];
[_splitViewCon addChildViewController: mainNaviCon];
self.window.rootViewController = _splitViewCon;

I searched web and found some keywords like "container view".
Is this something I have to do with, when I want to use a UISplitViewController on iPhone ?
I also watched WWDC Video, but I didn't understand "how to code it exactly".

Currently, I do not use any Interface Builder. So I'd be rather glad if someone gives programmaticaly way to code it.

Thank you !


回答1:


You can have side-by-side UISplitViewController on iPhones 4S, 5, 5S and 6 as well. To do it you have to embed its view in another view controller (addChildViewController:...didMoveToParentViewController:)

After that you'll be able to control split's behaviour by overriding its trait collection (setOverrideTraitCollection:forChildViewController:). Basically here you have to inspect your current trait collection and change the horizontal size class to regular. This way the UISplitController will be able to show both master and detail views (primary and secondary now called) by setting split's preferredDisplayMode

Then on rotation you can make the same observations about your trait collection and change the preferredDisplayMode and override again if necessary the split's trait collection. This can be done in viewWillTransitionToSize:withTransitionCoordinator: or willTransitionToTraitCollection:withTransitionCoordinator:. The second one won't be called on an iPad as its size classes are alway regular on both orientations.

One note about a problem I'm still not able to resolve. On iPhone 5S for example when rotating in portrait I'm hiding the master controller so to have only one view on the screen and the UISplitViewController should adapt itself to a UINavigationController. That works fine however during the rotation animation the master view is disappearing and you can see a blank ugly space.

One other note as well.
You have to implement UISplitViewControllerDelegate and use methods in order to set which view controller should be visible on app launch and when split is used as a navigation.
Here is a thread about this.

Hope it helps and if I find solution about the problem I have I'll update my answer




回答2:


The @user1006806 answer worked for me. Here's how I got rid of the ugly blank space during the rotation from within my UISplitViewController's rotation method (iOS 8):

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {

    UIInterfaceOrientation  theOrientation  = [[UIApplication sharedApplication] statusBarOrientation];
    if (UIInterfaceOrientationIsPortrait(theOrientation)) {
        self.preferredDisplayMode           = UISplitViewControllerDisplayModeAllVisible;
    } else {
        self.preferredDisplayMode           = UISplitViewControllerDisplayModeAutomatic;
    }

[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        self.preferredDisplayMode           = UISplitViewControllerDisplayModeAutomatic;
}];

[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

}



来源:https://stackoverflow.com/questions/26326428/uisplitviewcontroller-doesnt-work-on-iphone5-nor-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!