CRD*_*ave 74 iphone popup uiviewcontroller uialertview ios
由于对这个常见的反复问题没有完整,明确的答案,我会在这里提出并回答.
通常我们需要提供一个UIViewController不覆盖全屏的内容,如下图所示.

Apple提供了几个类似的UIViewController,如UIAlertViewTwitter或Facebook共享视图控制器等.
我们如何为自定义控制器实现此效果?
CRD*_*ave 94
注意:此解决方案在iOS 8中已中断.我将尽快发布新解决方案.
我将在这里使用故事板回答,但也可以没有故事板.
Init:UIViewController在故事板中创建两个.
FirstViewController哪个是正常的,SecondViewController哪个是弹出窗口.
莫代尔Segue公司:将UIButton在FirstViewController和创建这个SEGUE UIButton以SecondViewController莫代尔SEGUE.
Make Transparent:现在选择UIView(UIView默认情况下创建UIViewController)SecondViewController并更改其背景颜色以清除颜色.
制作背景暗淡:添加UIImageView在SecondViewController覆盖整个屏幕,并将其图像变暗一些半透明图像.您可以从此处获取示例:UIAlertView背景图像
显示设计:现在添加UIView并制作您想要显示的任何类型的设计.这是我的故事板的截图

SecondViewController弹出窗口,询问用户名和密码重要:现在迈出了主要的一步.我们希望它SecondViewController不会完全隐藏FirstViewController.我们设置了清晰的颜色,但这还不够.默认情况下,它会在模型显示后面添加黑色,因此我们必须在viewDidLoad中添加一行代码FirstViewController.您也可以在其他地方添加它,但它应该在segue之前运行.
[self setModalPresentationStyle:UIModalPresentationCurrentContext];
解雇:何时解雇取决于您的使用案例.这是一个模态演示,所以要解雇我们为模态演示做的事情:
[self dismissViewControllerAnimated:YES completion:Nil];
就这样.....
欢迎任何形式的建议和评论.
演示: 您可以从Here:Popup Demo获取演示源项目
新:有人在这个概念上做得非常好:MZFormSheetController
新:我发现了另外一个代码来获得这种功能:KLCPopup
iOS 8更新:我使这个方法适用于iOS 7和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];
}
}
Run Code Online (Sandbox Code Playgroud)
可以像这样在prepareForSegue中使用这个方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
PopUpViewController *popup = segue.destinationViewController;
[self setPresentationStyleForSelfController:self presentingController:popup]
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ens 47
在您想要作为模态弹出窗口的ViewController上,使根UIView的背景颜色清晰.
提示:不要使用根UIView作为弹出窗口.添加一个较小的新UIView作为弹出窗口.
为具有弹出窗口的ViewController创建一个Segue.选择"以模态呈现".

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

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

这两种方法都可行.应该这样做!
Sig*_*Sig 13
您可以在Interface Builder中执行此操作.
mie*_*tus 10
随意使用我的表格控制器MZFormSheetController for iPhone,在示例项目中有很多关于如何呈现模态视图控制器的例子,它不会覆盖整个窗口并且具有许多演示/过渡样式.
您还可以尝试最新版本的MZFormSheetController,它被称为MZFormSheetPresentationController并具有更多功能.