Since there is no complete, definitive answer to this common recurring question, I'll ask and answer it here.

我们经常需要展示UIViewController,这样它就不会覆盖整个屏幕,如下图所示.

在此处输入图像描述

苹果提供了几个类似的UIViewController,如UIAlertView、Twitter或Facebook共享视图控制器等.

How can we achieve this effect for a custom controller?

推荐答案

NOTE : This solution is broken in iOS 8. I will post new solution ASAP.

我将在这里使用故事板回答,但是没有故事板也是可能的.

  1. Init:创建两个UIViewController英寸的故事板.

    • lets say FirstViewController which is normal and SecondViewController which will be the popup.

  2. Modal Segue:UIButton放入FirstViewController,并在这UIButtonSecondViewController之间创建一个段作为模式段.

  3. Make Transparent:现在 Select SecondViewControllerUIView(默认情况下使用UIViewController创建UIView),并将其背景 colored颜色 更改为透明 colored颜色 .

  4. Make background Dim:添加覆盖整个屏幕的UIImageView英寸SecondViewController,并将其图像设置为某个暗淡的半透明图像.你可以从这里拿到样品:UIAlertView Background Image.

  5. Display Design:现在加上UIView,你可以做任何你想展示的设计.这是我的故事板的屏幕截图 情节提要

    • 这里我有Add Segue on login按钮,它会以弹出方式打开SecondViewController,询问用户名和密码
  6. Important:现在是主要步骤.我们希望SecondViewController不会完全隐藏FirstViewController.我们设置了清晰的 colored颜色 ,但这还不够.默认情况下,它会在模型演示文稿后面添加黑色,因此我们必须在viewDidLoad FirstViewController中添加一行代码.你也可以在其他地方添加它,但它应该在segue之前运行.

    [self setModalPresentationStyle:UIModalPresentationCurrentContext];

  7. Dismiss:何时解雇取决于您的用例.这是一个模态演示文稿,因此,为了不考虑,我们按照我们对模态演示文稿所做的操作进行操作:

    [self dismissViewControllerAnimated:YES completion:Nil];

仅此而已.....

欢迎提出任何建议和意见.

Demo : 您可以从此处获得演示源代码项目:Popup Demo

NEW : Someone have done very nice job on this concept : MZFormSheetController
New : I found one more code to get this kind of function : KLCPopup


iOS 8 Update : I made this method to work with both iOS 7 and iOS 8

+ (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];
    }
}

可以在prepaareForSegue内部使用此方法,如下所示

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    PopUpViewController *popup = segue.destinationViewController;
    [self setPresentationStyleForSelfController:self presentingController:popup]
}

Ios相关问答推荐

(Flutter)获取图像从图像拾取器保存和加载问题

缺少预期的键:';NSPrival yCollectedDataTypes';

PieChartView未以编程方式显示在屏幕上

有没有办法观察滚动的SwiftUI图表的内容偏移量?

自定义油漆圆角三角形

通过拖动更改视图位置的SwiftUI会引发UI错误

如何将模糊输入/模糊输出添加到SwiftUI中的非对称过渡?

为什么iOS委派在KMM中不能正常工作?

使用按位|带有布尔操作数

UIView 倒置旋转时的zoom 问题

工具栏底部 iOS 16 - SwiftUI

SwiftUI 图像与下拉视图切换一起 skip

将角半径仅应用于 Swiftui 中按钮的两个角

DateFormatter 使用 TimeZone 和 Locale 的特定组合返回 nil

Xcode 8,iOS 10 -为进程启动 WebFilter 日志(log)记录

实现一个将块用作回调的方法

setNeedsLayout 和 setNeedsDisplay

iOS中的apk类似功能是什么?

关闭通过模态 segue 显示的视图

iOS13状态栏背景 colored颜色 与大文本模式下的导航栏不同