プログラムでsplitviewcontrollerのマスタービューを非表示にする方法はありますか?私のアプリケーションでは、最初の画面はsplitviewcontrollerであり、次の画面に分割ビューは必要ありません。どうすればこれを達成できますか
9 に答える
SDK 5.0では、UISplitViewControllerDelegateの新しいメソッドが追加され、これを簡単に実行できるようになりました。次のように実装するだけで、マスタービューは表示されません。
- (BOOL)splitViewController:(UISplitViewController*)svc
shouldHideViewController:(UIViewController *)vc
inOrientation:(UIInterfaceOrientation)orientation
{
return YES;
}
それを見ることができる唯一の場所は回転です-マスタービューの一部はアニメーション中に表示されます。私はそれを簡単な方法で修正しました。マスターに空の黒のビューをロードするだけです。
PS:これがi0707で実際のものかどうかはわかりません。しかし、これが現在同じ問題を抱えている他の人々に役立つことを願っています。
ジャックからの回答と同じですが、ライナーが1つあります。過去-(void)setDetailItem:(id)newDetailItem {...}に移動して、マスターを閉じます。
[[UIApplication sharedApplication] sendAction: self.navigationItem.leftBarButtonItem.action
to: self.navigationItem.leftBarButtonItem.target
from: nil
forEvent: nil];
SplitViewControllerによって提供されるBarButtonItemは、マスタービューコントローラーをプログラムで非表示にするための鍵です。
このコードは危険です!しかしエレガント:)
ObjectiveCメッセージライブラリをインポートする
#import <objc/message.h>
次に、SplitViewControllerによって提供されるUIBarButtonItemへのハンドルを取得します
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem
forPopoverController:(UIPopoverController *)popoverController
{
barButtonItem.title = @"Master";
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
//Obtain handle to BarButtonItem
[self setMasterButtonItem:barButtonItem];
}
次に、イベントが発生すると、マスタービューコントローラーの自動終了がトリガーされます。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
あなたはこれを行うことができます
objc_msgSend(self.masterButtonItem.target, self.masterButtonItem.action);
Matt Gemmellは、「MGSplitViewController」と呼ばれる優れたカスタムsplitViewControllerを作成しました。これは非常に簡単に実装され、コメントが多く、通常のsplitViewControllerにはない多くの優れた機能が含まれています(ランドスケープビューでマスタービューを非表示にし、ランドスケープビューでスプリットの配置を変更し、ユーザーが実行時にスプリットのサイズをスムーズに変更できるようにします。等)。
情報とデモ: http: //mattgemmell.com/2010/08/03/mgsplitviewcontroller-updated/
ソースに直接:https ://github.com/mattgemmell/MGSplitViewController/
上記のコードは私にはうまくいきませんでした、しかし、私が試したとき
CGRect selfFrame = self.splitViewController.view.frame;
しました。だから....これは私のために働く..(このコードはあなたのdetailviewcontrollerにあるはずです)
-(void)hideMaster {
UIViewController *masterController = GetAppDelegate().masterController;
CGRect selfFrame = self.splitViewController.view.frame;
CGFloat aWidth = masterController.view.frame.size.width;
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.30f];
if(orientation == UIDeviceOrientationLandscapeLeft)
{
selfFrame.size.height += aWidth;
selfFrame.origin.y -= aWidth;
}
else if(orientation == UIDeviceOrientationLandscapeRight)
{
selfFrame.size.height += aWidth;
}
[self.splitViewController.view setFrame:selfFrame];
[UIView commitAnimations];
}
回転を許可するには、これが必要でした。
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration
{
[super willAnimateRotationToInterfaceOrientation:interfaceOrientation duration:duration]; // not sure if needed
//By the time this method is called the bounds of the view have been changed
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
if (masterIsHidden) {
[self showMaster];
}
} else {
if (self.editing) {
[self hideMaster];
}
}
[UIView setAnimationDuration:duration];
[UIView commitAnimations];
}
これを試して:
splitVC.modalPresentationStyle = UIModalPresentationFullScreen;
[splitVC presentModalViewController:[[splitVC viewControllers] objectAtIndex:1] animated:NO];
私のために4.2で動作します!
これが機能するもう1つの素晴らしいトリックです。ビデオリンクはこちらです。
トランジションは適切ではありませんが(申し訳ありませんが)、ルートビューを詳細ビューコントローラーのビューに設定してから、ビューをUISplitViewと交換し、詳細ビューをUISplitViewに移動することでこれを行うことができます。(実際には、ビュースワップ(プッシュ/フリップなど)をアニメーション化できる場合がありますが、ビュー変更アニメーション中に何かを変更することはお勧めできません。詳細ビューをUISplitView内に移動すると適格になる場合があります。)
これがあなたが探しているものであるかどうかわからない。たとえば、ボタンがクリックされたときに横向きモードでマスタービューを非表示にするには、次の操作を実行できます(セレクターメソッドで)
UIViewController *master = [splitViewController.viewControllers objectAtIndex:0];
UIViewController *detail = [splitViewController.viewControllers objectAtIndex:1];
[master.view setFrame:CGRectMake(0, 0, 0, 0)];
detail.view.frame = splitViewController.view.bounds; // or use master and detail length
これは機能します:
たとえば、「非表示」メソッドをボタンにアタッチします。
UIBarButtonItem *hideButton = [[UIBarButtonItem alloc] initWithTitle:@"hide"
style: UIBarButtonItemStylePlain
target: self
action: @selector(hide:)
];
[self.mainView.navigationItem setLeftBarButtonItem:hideButton];
このコードでは、「self.mainView」は、スプリットビューの2番目のビューにあるナビゲーションコントローラーのビューコントローラーであり、参照として使用されます。
hiddenメソッドはそのように見えます。
-(void)hide:(id)sender
{
UIViewController *masterController = [self.viewControllers objectAtIndex:0];
CGRect selfFrame = self.view.frame;
CGFloat aWidth = masterController.view.frame.size.width;
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.30f];
if(orientation == UIDeviceOrientationLandscapeLeft)
{
selfFrame.size.height += aWidth;
selfFrame.origin.y -= aWidth;
}
else if(orientation == UIDeviceOrientationLandscapeRight)
{
selfFrame.size.height += aWidth;
}
[self.view setFrame:selfFrame];
[UIView commitAnimations];
}
これが出発点です。明らかに、回転を処理したり、再度表示したりするために、より多くのロジックを実行する必要があります...
これがお役に立てば幸いです。
iOS5およびXcode4.3でテスト済み