如何使用在自适应故事板中关闭iPhone弹出窗口 [英] How to use dismiss an iPhone popover in an Adaptive Storyboard

查看:142
本文介绍了如何使用在自适应故事板中关闭iPhone弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的新手,我正在尝试同时学习故事板,Swift和iOS 8的新功能。

I am new to iOS development, and am trying to learn storyboarding, Swift, and the new features of iOS 8 at the same time.

我创建了一个非常简单的故事板,使用Popover演示文稿segue来显示另一个视图。在模拟器上,如果我为iPad运行它,它按预期工作。但是,如果我为iPhone而不是弹出窗口运行它,它会在原始视图的顶部显示全屏视图。这可以;然而,没有办法解雇它并回到原来的屏幕。

I have created a very simple storyboard that uses a Popover presentation segue to display another view. On the simulator, if I run this for an iPad, it works as expected. However, if I run it for an iPhone, instead of a popover, it displays a full-screen view, on top of the original view. This is fine; however, there is no way to dismiss it and go back to the original screen.

我观看了WWDC 2014视频228 A Look inside presentation controllers,他们可以如果他们完全使用代码构建用户界面,则显示一个解除按钮。

I have watched the WWDC 2014 video "228 A Look inside presentation controllers" and they can show a dismiss button if they build the user interface entirely with code.

我还看过411界面构建器中的新功能会话,他们说这可以在Interface Builder中完成,但他们没有显示它,承诺在实验室中展示如何做,如果有人感兴趣。不幸的是,我没有参加WWDC 2014,或者知道有人。我的Google搜索也没有返回任何有用的内容。

I have also watched the "411 What's new in interface builder" session, where they say that this can be done in Interface Builder, but they do not show it, promising to show how to do it in the lab, if anyone is interested. Unfortunately, I did not attend WWDC 2014, or know anyone who has. My Google searches have not returned anything helpful either.

推荐答案

您可以像这样添加导航控制器 -

You could add the navigation controller like this-


  • 将弹出视图控制器设置为导航控制器的根视图控制器。

  • 删除当前正在使用的popover segue

  • 将segue从显示弹出窗口的按钮重新连接到导航控制器。
    在iPad上你会得到一个popover,在iPhone上你会得到一个模态演示。 iPad和iPhone都会显示导航控制器。根据您的使用情况,这可能是您想要的,也可能不是。这是一个故事板应该是什么样子的屏幕显示。



    如果你真的想要你的查看控制器永远是一个弹出窗口离开你的故事板的方式,并添加这样的东西到你的视图控制器,呈现popover-

    If you really do want your view controller to always be a popover leave your storyboard the way it is and add something like this to your view controller that presents the popover-

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if ([segue.identifier isEqualToString:@"Your segue name"]) {
        UIViewController *yourViewController =  segue.destinationViewController;
        yourViewController.modalPresentationStyle = UIModalPresentationPopover;
        UIPopoverPresentationController *popoverPresentationController = yourViewController.popoverPresentationController;
        popoverPresentationController.delegate = self;
       }
    }
    

    呈现弹出框的视图控制器还需要响应到这个 UIPopoverPresentationDelegate 方法

    The view controller presenting the popover will also need to respond to this UIPopoverPresentationDelegate method

    - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller 
    {
    return UIModalPresentationNone;//always popover.
    }
    

    最后,您可以执行以下操作,仅将导航控制器添加到模态在iPhone上显示您的视图控制器,并在没有导航控制器的情况下将弹出框保留在iPad上。

    Lastly, you could do the following to only add the navigation controller to the modal presentation of your view controller on the iPhone and leave the popover on iPad without a navigation controller.


    • 保持故事板不变。

    • 注入导航控制器的正确位置是 - (UIViewController *)presentationController:(UIPresentationController *)controller viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style 。为了调用它,我们必须将自己设置为 UIPopoverPresentationController 的委托。
      再一次,我们将在 prepareForSegue中执行此操作:

    • Leave your storyboard as is.
    • The proper place to inject the navigation controller is in - (UIViewController *)presentationController:(UIPresentationController *)controller viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style. In order for this to be called we must set ourselves as the delegate of the UIPopoverPresentationController. Once again we will do this in prepareForSegue:

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if ([segue.identifier isEqualToString:@"Your segue name"]) {
        UIViewController *yourViewController =  segue.destinationViewController;
        yourViewController.modalPresentationStyle = UIModalPresentationPopover;
        UIPopoverPresentationController *popoverPresentationController = yourViewController.popoverPresentationController;
        popoverPresentationController.delegate = self;
        }
    }
    


  • 然后我们将在上面提到的委托方法中执行此操作

    Then we will do this in the delegate method that I mentioned above

    -(UIViewController *)presentationController:(UIPresentationController *)controller viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style
    {
        UIViewController *presentedViewController = controller.presentedViewController;
        UINavigationController *navigationController = [[UINavigationController alloc]
                     initWithRootViewController:presentedViewController];
        UIBarButtonItem *dismissButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:@selector(done:)];
        presentedViewController.navigationItem.rightBarButtonItem = dismissButton;
    
        return navigationController;
    }
    

    祝你好运!

    这篇关于如何使用在自适应故事板中关闭iPhone弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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