隐藏/取消隐藏主视图控制器在横向中与纵向相同 [英] Make hide/unhide master view controller work in landscape the same as portrait

查看:114
本文介绍了隐藏/取消隐藏主视图控制器在横向中与纵向相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个简单的应用程序中使用拆分视图控制器。将一切都保留为默认工作正常。换句话说,当按下后退按钮时,主视图控制器始终以横向显示并以纵向覆盖详细视图控制器。

I am using a split view controller in a simple app. Leaving everything as default works fine. In other words, the master view controller always shows in landscape and overlays the detail view controller in portrait when the back button is pressed.

我想要做的是制作主视图控制器模仿横向中的相同功能,就像在纵向中一样。换句话说,当设备处于横向状态时,我希望隐藏主视图控制器,直到我点击后退按钮,然后我希望它覆盖详细视图控制器。

What I wanted to do was make the master view controller mimic the same functionality in landscape as it does in portrait. In other words, when the device is in landscape, I want the master view controller to be hidden until I hit the back button and then I want it to overlay the detail view controller.

我认为最好的方法是使用以下代码:

I figured the best way to do this was to use the following code:

 - (BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:     (UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
 {
     return self.bHideMaster;
 }

这是因为它以横向模式隐藏了主视图控制器。然后我使用以下代码重新出现:

This worked in that it hid the master view controller in landscape mode. I then used the following code to make it reappear:

 - (void)hideUnhidePagesController:(id)sender
 {
     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationDuration:0.30f];

     UISplitViewController* spv = self.splitViewController;

     // Change hide to unhide or vica versa
     self.bHideMaster= !self.bHideMaster;

     // Hide the button if master is visible
     if(self.bHideMaster)
     {
         self.navigationItem.leftBarButtonItem = self.pagesBarButton;
     }
     else
     {
          self.navigationItem.leftBarButtonItem = nil;
     }

     [spv.view setNeedsLayout];
     [spv willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
     [[self.splitViewController.viewControllers lastObject] view].frame = self.splitViewController.view.frame;
     [UIView commitAnimations];
 }

此ALMOST有效。我有2个问题:

This ALMOST worked. I have 2 problems:


  1. 从隐藏到取消隐藏和取消隐藏主视图控制器的转换不是'动画并且非常明显。我添加了动画代码(见上文),但它只动画了详细视图控制器,而不是主视图控制器。主机立即出现并消失(留下黑框消失),直到细节视图控制器滑过。

  1. The transition from hide-to-unhide and unhide-to-hide the master view controller isn't animated and is much to stark. I added animation code (see above) but it only animates the detail view controller and not the master view controller. The master appears and dis-appears instantly (leaving a black box on disappear) until the detail view controller slides over.

这也显示了我的第二个问题。我希望主视图控制器在横向模式下显示时与详细视图控制器重叠,使详细视图控制器保持原样。相反,它调整了细节视图控制器的大小(与我在开始之前在横向模式下的工作方式相同)。我希望主视图控制器以与纵向模式相同的方式进行交互:主视图在细节控制器的顶部滑动,并在选择项目时滑出。

This also shows my second problem. I want the master view controller to overlap the detail view controller when it appears in landscape mode, leaving the detail view controller as is. Instead, it resizes the detail view controller (the same way it does in landscape mode before I started). I want the master view controller to interact the same way it does in portrait mode: Master slides in over the top of the detail controller and slides back out when it an item is selected.

如果我可以解决问题2,那么我不必担心问题1.看起来拆分视图控制器中应该有一个方法会滑动在左侧的主设备中(与详细视图控制器重叠)。它以纵向模式执行,因此代码必须在那里。如何在横向模式下调用相同的代码?

If I could solve problem 2, then I don't have to worry about problem 1. It seems like there should be a method in the split view controller that would slide in the master from the left (overlapping the detail view controller). It does it in portrait mode so the code must be there. How can I call that same code in landscape mode?

谢谢!

---- -----编辑1 ---------

我已经重构了hideUnhidePagesController并且越来越近了。窗口现在覆盖纵向和横向。如果主轴在旋转时可见,则仍然存在问题。它会混淆并反转预期的行为。我在做这个工作。这里修改后的代码:

I have refactored hideUnhidePagesController and am getting closer. The window now overlays in both portrait and landscape. There is still a problem if the master is visible on rotation. It gets confused and inverts the expected behavior. I'm working on it. Here the amended code:

 - (void)hideUnhidePagesController:(id)sender
{
    // Change hide to unhide or vica versa
    self.bMasterIsHidden= !self.bMasterIsHidden;

    NSArray *controllers = self.splitViewController.viewControllers;
    UIViewController *rootViewController = [controllers objectAtIndex:0];
    UIView *rootView = rootViewController.view;
    CGRect rootFrame = rootView.frame;
    if(self.bMasterIsHidden)
    {
        rootFrame.origin.x -= rootFrame.size.width;
    }
    else
    {
        rootFrame.origin.x += rootFrame.size.width;
    }
    [UIView beginAnimations:@"hideUnhideView" context:NULL];
    rootView.frame = rootFrame;
    [UIView commitAnimations];
 }


推荐答案

我正在投入我最终使用的代码。希望这有助于其他人。

I'm putting in the code that I ended up using. Hope this helps someone else.

// ***************************************************************************************************
//
//  hideUnhideMasterViewControllerButtonPressed
//
// ***************************************************************************************************
- (void)hideUnhideMasterViewControllerButtonPressed:(id)sender {
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else {
        if(bMasterIsHidden)
            [self hideMasterViewController:NO];
        else
            [self hideMasterViewController:YES];
    }
}

// ***************************************************************************************************
//
//  hideMasterViewController
//
// ***************************************************************************************************
- (void)hideMasterViewController:(BOOL)bHideMaster {
    // Change hide to unhide or vica versa
    self.bMasterIsHidden= !self.bMasterIsHidden;

    NSArray *controllers = self.splitViewController.viewControllers;
    UIViewController *rootViewController = [controllers objectAtIndex:0];
    UIView *rootView = rootViewController.view;
    CGRect rootFrame = rootView.frame;
    if(bHideMaster) {
        if(self.tapRecognizer) {
            rootFrame.origin.x -= rootFrame.size.width;
            [self.view removeGestureRecognizer:self.tapRecognizer];
            self.tapRecognizer = nil;
        }
    }
    else {
        rootFrame.origin.x += rootFrame.size.width;
        self.tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
        self.tapRecognizer.numberOfTapsRequired = 1;
        [self.view addGestureRecognizer:self.tapRecognizer];
        self.tapRecognizer.delegate = self;
    }
    // Log resulting frame
    NSString *hiddenString = self.bMasterIsHidden ? @"YES" : @"NO";
    NSLog(@"Page=%@   Class=%@  MasterIsHidden=%@ Origin(x,y)=(%f, %f) Size(width,height)=(%f, %f)", self.pageDefinition.pageName, [self class], hiddenString, rootFrame.origin.x, rootFrame.origin.y, rootFrame.size.width, rootFrame.size.height);

    [UIView beginAnimations:@"hideUnhideView" context:NULL];
    rootView.frame = rootFrame;
    [UIView commitAnimations];
}

这篇关于隐藏/取消隐藏主视图控制器在横向中与纵向相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆