2012-03-16

UISplitViewでデバイスが縦のときもMasterViewを表示する

iOS5.0以上であれば、UISplitViewControllerDelegateに、次のコードを書き足すだけで、デバイスが縦のときにもUISplitViewを2ペインにできます。
- (BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
    return NO;
}

右側のDetailViewが狭くなるのがやはり気になるので、左側のMasterViewの表示/非表示を切り替えられるボタンを付けたら使いやすくなるような気がするのですが、このやり方は調査中です。

splitViewController:shouldHideViewController:inOrientation: が使えるのはiOS5.0+なので、iOS4.xでは、普通にMasterViewが消えてしまいます。
そこで、iOSのバージョンをチェックして、iOS4.xならば、スクリーンの向きを縦にできないようにします。
NSString* systemVersion = [[UIDevice currentDevice] systemVersion];
で「4.3.2」や「5.0.1」などの文字列を取得できるので、これを見てshouldAutorotateToInterfaceOrientation:の戻り値を決めます。


すごく便利そうなライブラリがオープンソースで公開されています。

0 件のコメント: