NOTE : This solution is broken in iOS 8. I will post new solution ASAP.
我将在这里使用故事板回答,但是没有故事板也是可能的.
Init:创建两个UIViewController
英寸的故事板.
- lets say
FirstViewController
which is normal and SecondViewController
which will be the popup.
Modal Segue:将UIButton
放入FirstViewController,并在这UIButton
到SecondViewController
之间创建一个段作为模式段.
Make Transparent:现在 Select SecondViewController
的UIView
(默认情况下使用UIViewController
创建UIView
),并将其背景 colored颜色 更改为透明 colored颜色 .
Make background Dim:添加覆盖整个屏幕的UIImageView
英寸SecondViewController
,并将其图像设置为某个暗淡的半透明图像.你可以从这里拿到样品:UIAlertView
Background Image.
Display Design:现在加上UIView
,你可以做任何你想展示的设计.这是我的故事板的屏幕截图
- 这里我有Add Segue on login按钮,它会以弹出方式打开
SecondViewController
,询问用户名和密码
Important:现在是主要步骤.我们希望SecondViewController
不会完全隐藏FirstViewController.我们设置了清晰的 colored颜色 ,但这还不够.默认情况下,它会在模型演示文稿后面添加黑色,因此我们必须在viewDidLoad FirstViewController
中添加一行代码.你也可以在其他地方添加它,但它应该在segue之前运行.
[self setModalPresentationStyle:UIModalPresentationCurrentContext];
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]
}