How can I set the Inital Orientation for a UISplit

2019-05-30 17:01发布

I have a split view app running fine on the iPad. It is using the default setup (Popover in Portrait, table view on the left in landscape). The views shift correctly once the app is running. The problem I am seeing is that when the app starts (in the simulator) running in landscape mode the UI paradigm is the one intended for Portrait mode (Master List is a popover) is what loads.

I am thinking this is some strangeness with the simulator, or I am missing an option on my main view controller.

4条回答
等我变得足够好
2楼-- · 2019-05-30 17:14

I ran into the same problem as is described here. The solution was, embarrassingly, as simple as manually setting the view's frame before adding it to the window.

Just check the interface orientation and, if it's landscape, switch the application frame width and height dimensions (i.e., width becomes height, height becomes width).

CGRect frame = [[UIScreen mainScreen] applicationFrame];

switch(controller.interfaceOrientation){
    case UIInterfaceOrientationPortrait:
    case UIInterfaceOrientationPortraitUpsideDown:
      [controller.view setFrame:frame];
      break;
    case UIInterfaceOrientationLandscapeLeft:
    case UIInterfaceOrientationLandscapeRight:
      [controller.view setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.height, frame.size.width)];
      break;
}
查看更多
可以哭但决不认输i
3楼-- · 2019-05-30 17:23

I succeeded into displaying a loading view by doing

[window addSubview:self._splitViewController.view];
[window addSubview:self._myLoadingView];
self._splitViewController.view.hidden = YES;
[window makeKeyAndVisible];
[self loadAllDatas];
self._splitViewController.view.hidden = NO;

i works fine

查看更多
Explosion°爆炸
4楼-- · 2019-05-30 17:26

It doesn't work correctly because the default detail view controller of UISplitViewController is just a plain UIViewController. The shouldRotate method of UIViewController returns YES for portrait mode only.

Adding a generic UIViewController returning YES in shouldRotate for all orientations solved the problem for me.

查看更多
三岁会撩人
5楼-- · 2019-05-30 17:34

Adding this as an answer as well in the hopes it will be more apparent to those in need of the same fix.

I solved this. I was waiting for an external XML stream to be loaded & parsed. As a result I was loading the window with the splitViewController view AFTER my applicationDidFinishLaunching method.

Adding:

[window addSubview: splitViewController.view]; 
[window makeKeyAndVisible]; 

back into that method fixed the orientation recognition

查看更多
登录 后发表回答