本文介绍了在iPhone中将UIViewController显示为Popup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 由于对这个常见的反复问题没有完整,明确的答案,我会在这里提出并回答。 我们经常会需要提供一个 UIViewController ,这样它就不会覆盖全屏,如下图所示。 Apple提供了几个类似的 UIViewController ,例如 UIAlertView ,Twitter或Facebook共享视图控制器等。 我们如何为自定义控制器实现此效果? 解决方案 注意:此解决方案在iOS 8中已被破解。我将尽快发布新解决方案。 我将使用故事板回答此问题但也有可能没有故事板。 初始值:创建两个 UIViewController 在故事板中。 让我们说 FirstViewController 这是正常的, SecondViewController 这将是弹出窗口。 Modal Segue:放入在FirstViewController中使用UIButton 并在此 UIButton 上创建一个segue到 SecondViewController 作为模态segue。 / p> 透明:现在选择 UIView ( UIView 默认情况下使用 UIViewController 创建 SecondViewController 并将其背景颜色更改为明确的颜色。 让背景暗淡:在中添加 UIImageView code> SecondViewController ,它涵盖整个屏幕并将其图像设置为一些暗淡的半透明图像。您可以从此处获取样本: 新:有人有在这个概念上做得非常好: MZFormSheetController 新:我找到了一个更多代码来获得这种功能: KLCPopup iOS 8更新:我让这个方法适用于iOS 7和iOS 8 +(void)setPresentationStyleForSelfCont roller:(UIViewController *)selfController presentsController:(UIViewController *)presentsController { if(iOSVersion> = 8.0) { presentingController.providesPresentationContextTransitionStyle = YES; presentsController.definesPresentationContext = YES; [presentsController setModalPresentationStyle:UIModalPresentationOverCurrentContext]; } else { [selfController setModalPresentationStyle:UIModalPresentationCurrentContext]; [selfController.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext]; } } 可以在prepareForSegue中使用此方法像这样进行解析 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { PopUpViewController * popup = segue.destinationViewController; [self setPresentationStyleForSelfController:self presentsController:popup] } Since there is no complete, definitive answer to this common recurring question, I'll ask and answer it here.Often we need to present a UIViewController such that it doesn't cover full screen, as in the picture below.Apple provides several similar UIViewController, such as UIAlertView, Twitter or Facebook share view controller, etc..How can we achieve this effect for a custom controller? 解决方案 NOTE : This solution is broken in iOS 8. I will post new solution ASAP.I am going to answer here using storyboard but it is also possible without storyboard.Init: Create two UIViewController in storyboard.lets say FirstViewController which is normal and SecondViewController which will be the popup.Modal Segue: Put UIButton in FirstViewController and create a segue on this UIButton to SecondViewController as modal segue.Make Transparent: Now select UIView (UIView Which is created by default with UIViewController) of SecondViewController and change its background color to clear color.Make background Dim: Add an UIImageView in SecondViewController which covers whole screen and sets its image to some dimmed semi transparent image. You can get a sample from here : UIAlertView Background ImageDisplay Design: Now add an UIView and make any kind of design you want to show. Here is a screenshot of my storyboardHere I have add segue on login button which open SecondViewController as popup to ask username and passwordImportant: Now that main step. We want that SecondViewController doesn't hide FirstViewController completely. We have set clear color but this is not enough. By default it adds black behind model presentation so we have to add one line of code in viewDidLoad of FirstViewController. You can add it at another place also but it should run before segue.[self setModalPresentationStyle:UIModalPresentationCurrentContext];Dismiss: When to dismiss depends on your use case. This is a modal presentation so to dismiss we do what we do for modal presentation:[self dismissViewControllerAnimated:YES completion:Nil];Thats all.....Any kind of suggestion and comment are welcome.Demo :You can get demo source project from Here : Popup DemoNEW : Someone have done very nice job on this concept : MZFormSheetControllerNew : 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]; }}Can use this method inside prepareForSegue deligate like this- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { PopUpViewController *popup = segue.destinationViewController; [self setPresentationStyleForSelfController:self presentingController:popup]} 这篇关于在iPhone中将UIViewController显示为Popup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!