在iPhone中将UIViewController显示为Popup

时间:2013-04-26 07:13:05

标签: ios iphone uiviewcontroller popup uialertview

由于对这个常见的反复出现的问题没有完整,明确的答案,我会在这里提出并回答。

通常我们需要提供一个UIViewController,使其不会覆盖全屏,如下图所示。

enter image description here

Apple提供了几个类似的UIViewController,例如UIAlertView,Twitter或Facebook共享视图控制器等。

我们如何为自定义控制器实现此效果?

7 个答案:

答案 0 :(得分:94)

注意:此解决方案在iOS 8中已中断。我将尽快发布新解决方案。

我将使用故事板回答这里,但也可以不使用故事板。

  1. 初始化:在故事板中创建两个UIViewController

    • 让我们说FirstViewController这是正常的,SecondViewController这将是弹出窗口。

  2. 模态搜索:UIButton放入FirstViewController并在此UIButtonSecondViewController上创建一个segue作为模态segue。

    < / LI>
  3. 透明化:现在选择UIView的{​​{1}}(UIView默认情况下创建的UIViewController)并更改背景颜色以清除颜色。

  4. 制作背景暗淡:在SecondViewController中添加UIImageView,覆盖整个屏幕,并将其图像设置为一些暗淡的半透明图像。您可以从此处获取示例:UIAlertView Background Image

  5. 展示设计:现在添加SecondViewController并制作您想要展示的任何设计。这是我的故事板的截图 storyboard

    • 这里我在登录按钮上添加segue,打开UIView作为弹出窗口询问用户名和密码
  6. 重要提示:现在是主要步骤。我们希望SecondViewController不会完全隐藏FirstViewController。我们设置了清晰的颜色,但这还不够。默认情况下,它会在模型​​表示后面添加黑色,因此我们必须在SecondViewController的viewDidLoad中添加一行代码。您也可以在其他地方添加它,但它应该在segue之前运行。

    FirstViewController

  7. 解雇:何时解雇取决于您的使用案例。这是一个模态演示,所以要忽略我们为模态演示做的事情:

    [self setModalPresentationStyle:UIModalPresentationCurrentContext];

  8. 多数人......

    欢迎任何形式的建议和评论。

    演示: 您可以从Here获取演示源项目:Popup Demo

    :有人在这个概念上做得很好:MZFormSheetController
    :我发现了另外一个代码来实现这种功能:KLCPopup


      

    iOS 8更新:我让这个方法适用于iOS 7和iOS 8   

    [self dismissViewControllerAnimated:YES completion:Nil];

    可以在prepareForSegue中使用此方法,像这样使用

    + (void)setPresentationStyleForSelfController:(UIViewController *)selfController presentingController:(UIViewController *)presentingController
    {
        if (iOSVersion >= 8.0)
        {
            presentingController.providesPresentationContextTransitionStyle = YES;
            presentingController.definesPresentationContext = YES;
    
            [presentingController setModalPresentationStyle:UIModalPresentationOverCurrentContext];
        }
        else
        {
            [selfController setModalPresentationStyle:UIModalPresentationCurrentContext];
            [selfController.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext];
        }
    }
    

答案 1 :(得分:45)

Interface Builder(故事板)中的模态弹出窗口

第1步

在想要作为模态弹出窗口的ViewController上,清除根UIView的背景颜色。 Set Clear Color on root view 提示:不要使用根UIView作为弹出窗口。添加一个较小的新UIView作为弹出窗口。

第2步

为具有弹出窗口的ViewController创建一个Segue。选择“以模态呈现”。 Segue

从这里创建弹出窗口的两种方法

方法一 - 使用Segue

选择Segue并将Presentation更改为“Over Current Context”: Over Current Context

方法二 - 使用视图控制器

选择作为弹出窗口的ViewController场景。在Attributes Inspector中的View Controller部分下,将Presentation设置为“Over Current Context”: ViewController

任何一种方法都可行。应该这样做!

Finished Product

答案 2 :(得分:10)

随意使用我的表格控制器MZFormSheetController for iPhone,在示例项目中有很多关于如何呈现模态视图控制器的示例,它不会覆盖整个窗口并且具有许多演示/过渡样式。

您还可以尝试最新版本的MZFormSheetController,它名为MZFormSheetPresentationController并具有更多功能。

答案 3 :(得分:2)

您可以使用EzPopup(https://github.com/huynguyencong/EzPopup),它是一个Swift pod并且非常易于使用:

// init YourViewController
let contentVC = ...

// Init popup view controller with content is your content view controller
let popupVC = PopupViewController(contentController: contentVC, popupWidth: 100, popupHeight: 200)

// show it by call present(_ , animated:) method from a current UIViewController
present(popupVC, animated: true)

答案 4 :(得分:1)

您可以执行此操作以将任何其他子视图添加到视图控制器。 首先将ViewController的状态栏设置为None,将其添加为子视图,以便您可以调整大小到任何您想要的大小。然后在Present View控制器中创建一个按钮和按钮单击的方法。在方法中:

- (IBAction)btnLogin:(id)sender {
    SubView *sub = [[SubView alloc] initWithNibName:@"SubView" bundle:nil];
    sub.view.frame = CGRectMake(20, 100, sub.view.frame.size.width, sub.view.frame.size.height);
    [self.view addSubview:sub.view];
}

希望这有帮助,随时询问是否有任何疑问......

答案 5 :(得分:1)

Imao把UIImageView放在背景上并不是最好的主意。在我的情况下,我添加了控制器视图其他2个视图。第一个视图在背景上有[UIColor clearColor],第二个颜色你想透明(在我的情况下是灰色)。注意顺序很重要。然后第二个视图设置alpha 0.5(alpha&gt; = 0&lt; = 1 )。将其添加到prepareForSegue

中的行
infoVC.providesPresentationContextTransitionStyle = YES;
infoVC.definesPresentationContext = YES;

就是这样。

答案 6 :(得分:0)

Swift 4:

添加叠加层或弹出窗口视图 您还可以使用容器视图来获取免费的View Controller(从通常的对象选项板/库中获取容器视图)

enter image description here

<强>步骤:

  1. 拥有一个包含此容器视图的视图(图片中的ViewForContainer),在显示容器视图的内容时将其调暗。连接第一个View Controller

  2. 内的插座
  3. 第一次加载VC时隐藏此视图

  4. 单击“按钮”时取消隐藏 enter image description here

  5. 要在显示容器视图内容时调暗此视图,请将视图背景设置为黑色,将不透明度设置为30%

  6. enter image description here

    单击按钮时,您将获得此效果 enter image description here