XCode 4.2で新しい分割ビューベースのプロジェクトを作成しました
次にDetailViewController.m
ファイルこのメソッドを追加しました
- (BOOL)splitViewController: (UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
//This method is only available in iOS5
return NO;
}
これで、splitviewコントローラーの左右両方の部分を一度に表示できるようになりました。
これで、UIBarButtonItem
ナビゲーションバーにDetailViewController
を追加しました。これを使用して、Portrairtモードとランドスケープモードの両方でマスタービューを非表示にしたり表示したりできます。
- (IBAction)hideUnhide:(id)sender
{
//How can hide & unhide
}
これどうやってするの?
instead spv.delegate=nil; spv.delegate=self;
次を行う必要があります:
[spv willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
「setNeedsLayout」は、UISplitViewControllerに「shouldHideViewController」を要求させる
- (IBAction)hideUnhide:(id)sender {
UISplitViewController* spv = ...;
self.hideMaster= !self.hideMaster;
[ spv.view setNeedsLayout ]
}
IOS 5.1では、次のようにする必要があります。
内部DetailViewController.m
- (IBAction)hideUnhide:(id)sender {
UISplitViewController* spv = ...;
self.hideMaster= !self.hideMaster;
[spv.view setNeedsLayout];
spv.delegate = nil;
spv.delegate = self;
}
- (BOOL)splitViewController:(UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation {
return self.hideMaster;
}
上記の応答を組み合わせて、IOS 6:
// In split delegate
-(void)hideMaster:(BOOL)hideState
{
_masterIsHidden = hideState;
[self.splitViewController.view setNeedsLayout];
self.splitViewController.delegate = nil;
self.splitViewController.delegate = self;
[self.splitViewController willRotateToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:0];
}
-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
return self.masterIsHidden;
}
-(IBAction)clickToShowMaster:(id)sender
{
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"prev.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToHidemaster:)];
self.navigationItem.leftBarButtonItem = systemItem1;
[self.tabBarController.tabBar setHidden:NO];
[self hideMaster:NO];
}
-(void)hideMaster:(BOOL)hideState
{
ishideMaster=hideState;
[self.splitViewController.view setNeedsLayout];
self.splitViewController.delegate = nil;
self.splitViewController.delegate = self;
[self.splitViewController willRotateToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:0];
}
-(void)hideMaster:(BOOL)hideState
{
ishideMaster=hideState;
[self.splitViewController.view setNeedsLayout];
self.splitViewController.delegate = nil;
self.splitViewController.delegate = self;
[self.splitViewController willRotateToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:0];
}
#pragma mark - Split view
-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
if(UIInterfaceOrientationIsPortrait(orientation))
{
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"down.png"] style:UIBarButtonItemStylePlain target:self action:@selector(showPopup)];
self.navigationItem.leftBarButtonItem = systemItem1;
[self setUIforPortrait];
return YES;
}
if (UIInterfaceOrientationIsLandscape(orientation))
{
if(ishideMaster==TRUE)
{
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"next.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToShowMaster:)];
self.navigationItem.leftBarButtonItem = systemItem1;
[self setUIForFullLandscape];
}
else
{
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"prev.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToHidemaster:)];
self.navigationItem.leftBarButtonItem = systemItem1;
[self setUIForHalfLandscape];
}
return ishideMaster;
}
}
//add the navigation button on left top, to pop-up master view.
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
[barButtonItem setImage:[UIImage imageNamed:@"down.png"]];
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"down.png"] style:UIBarButtonItemStylePlain target:self action:@selector(showPopup)];
self.navigationItem.leftBarButtonItem = systemItem1;
self.masterPopoverController = popoverController;
self.masterPopoverController.delegate=self;
}
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
// Called when the view is shown again in the split view, invalidating the button and popover controller.
//;
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
if(ishideMaster==FALSE)
{
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"prev.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToHidemaster:)];
self.navigationItem.leftBarButtonItem = systemItem1;
}
else
{
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"next.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToShowMaster:)];
self.navigationItem.leftBarButtonItem = systemItem1;
}
}
else if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
}
//self.masterPopoverController = nil;
}
IOS8ではこれは簡単です。
それを隠す
[UIView animateWithDuration:0.2 animations:^{
splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
} completion:nil];
それを表示するには
[UIView animateWithDuration:0.2 animations:^{
self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
} completion:nil];
デリゲートを再割り当てする必要がないというwzbozonのコメントに関して、私は2つの行が
self.splitViewController.delegate = nil;
self.splitViewController.delegate = self;
...シミュレーターでは必要ありませんでしたが、iOS5 iPad 1では必要でした。これらがないと、非表示/表示の動作は発生しませんでした(ボタンをクリックしてもマスタービューは折りたたまれません)。
displayModeButtonItem
のUISplitViewController
プロパティのアクションをトリガーすることにより、マスターViewControllerを表示/非表示にすることができます。
Swift
if let displayModeButtonItem = splitViewController?.displayModeButtonItem() {
displayModeButtonItem.target?.performSelector(displayModeButtonItem.action)
}
Objective-C
UIBarButtonItem *displayModeButtonItem = [self.splitViewController displayModeButtonItem];
[displayModeButtonItem.target performSelector: displayModeButtonItem.action];
代理人、向き、レイアウトを同時にいじるよりも、自分にふさわしいと感じています。
さて、あなたの質問の簡単な部分は、ブール値を使用して、プロパティhideMasterを言うことです、そして次に
- (IBAction)hideUnhide:(id)sender
{
self.hideMaster= !self.hideMaster;
}
その後...
- (BOOL)splitViewController: (UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
//This method is only available in iOS5
return self.hideMaster;
}
これは正常に機能しますが、shouldHideViewControllerは、回転時など、splitVCの再描画中にのみ呼び出されるため、マスターは非表示/再表示のみを行います。
Swift 3.
私は使いました
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let object = self.exercises[indexPath.row]
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
UIView.animate(withDuration: 0.2, delay: 0.0, options: [.curveEaseOut], animations: {
self.splitViewController?.preferredDisplayMode = .primaryHidden
}, completion: nil)
}
}
}