如何在Swift中的当前视图上呈现模态

时间:2023-01-23 22:43:19

(Xcode6, iOS8, Swift, iPad)

(Xcode6,iOS8,Swift,iPad)

I am trying to create a classic Web-like modal view, where the outside of the dialog box is "grayed-out." To accomplish this, I've set the alpha value of the backgroundColor of the view for the modal to 0.5, like so:

我正在尝试创建一个类似于Web的经典模态视图,其中对话框的外部是“灰显的”。为了实现这一点,我将模态的视图的backgroundColor的alpha值设置为0.5,如下所示:

self.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)

The only problem is that when the modal becomes full-screen, the presenting view is removed. (Ref Transparent Modal View on Navigation Controller).

唯一的问题是,当模态变为全屏时,将删除呈现视图。 (导航控制器上的参考透明模态视图)。

(A bit irritated at the concept here. Why remove the underlying view? A modal is, by definition, to appear atop other content. Once the underlying view is removed, it's not really a modal anymore. it's somewhere between a modal and a push transition. Wa wa wa... Anyway..)

(这里的概念有点恼火。为什么删除基础视图?根据定义,模态出现在其他内容之上。一旦基础视图被移除,它就不再是模态了。它介于模态和推送之间过渡。瓦娃......反正..)

To prevent this from happening, I've set the modalPresentationStyle to CurrentContext in the viewDidLoad method of the parent controller, and in Storyboard... but no luck.

为了防止这种情况发生,我将modalPresentationStyle设置为父控制器的viewDidLoad方法中的CurrentContext,并在Storyboard中......但没有运气。

    self.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
    self.navigationController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext

How do I prevent the presenting view from being removed when the modal becomes full screen?

当模态变为全屏时,如何防止显示视图被删除?

tyvm.. more info below.

tyvm ..更多信息如下。

Also in Storyboard, like so (Presentation: Current Context)

也在故事板中,如此(演示文稿:当前上下文)

如何在Swift中的当前视图上呈现模态

Thx for your help... documentation below:

感谢您的帮助...以下文档:

如何在Swift中的当前视图上呈现模态

6 个解决方案

#1


37  

First, remove all explicit setting of modal presentation style in code and do the following:

首先,删除代码中模态表示样式的所有显式设置,并执行以下操作:

  1. In the storyboard set the ModalViewController's modalPresentation style to Over Current context
  2. 在故事板中将ModalViewController的modalPresentation样式设置为Over Current上下文

如何在Swift中的当前视图上呈现模态

  1. Check the checkboxes in the Root/Presenting ViewController - Provide Context and Define Context. They seem to be working even unchecked.
  2. 检查Root / Presenting ViewController中的复选框 - 提供上下文和定义上下文。他们似乎工作甚至没有受到控制。

#2


12  

The only problem I can see in your code is that you are using CurrentContext instead of OverCurrentContext.

我在代码中可以看到的唯一问题是您使用的是CurrentContext而不是OverCurrentContext。

So, replace this:

所以,替换这个:

self.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
self.navigationController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext

for this:

为了这:

self.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
self.navigationController.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

#3


9  

You can try this code for Swift

您可以为Swift尝试此代码

 let popup : PopupVC = self.storyboard?.instantiateViewControllerWithIdentifier("PopupVC") as! PopupVC
 let navigationController = UINavigationController(rootViewController: popup)
 navigationController.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
 self.presentViewController(navigationController, animated: true, completion: nil)

#4


1  

I am updating a simple solution. First add an id to your segue which presents modal. Than in properties change it's presentation style to "Over Current Context". Than add this code in presenting view controller (The controller which is presenting modal).

我正在更新一个简单的解决方案首先在你的segue中添加一个id,它提供了模态。比在属性中将其呈现样式更改为“Over Current Context”。然后在呈现视图控制器(呈现模态的控制器)中添加此代码。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let Device = UIDevice.currentDevice()

    let iosVersion = NSString(string: Device.systemVersion).doubleValue

    let iOS8 = iosVersion >= 8
    let iOS7 = iosVersion >= 7 && iosVersion < 8
    if((segue.identifier == "chatTable")){

    if (iOS8){


          }
        else {

            self.navigationController?.modalPresentationStyle = UIModalPresentationStyle.CurrentContext


        }
    }
}

Make sure you change segue.identifier to your own id ;)

确保将segue.identifier更改为您自己的ID;)

#5


0  

The problem with setting the modalPresentationStyle from code was that you should have set it in the init() method of the presented view controller, not the parent view controller.

从代码设置modalPresentationStyle的问题是您应该在呈现的视图控制器的init()方法中设置它,而不是父视图控制器。

From UIKit docs: "Defines the transition style that will be used for this view controller when it is presented modally. Set this property on the view controller to be presented, not the presenter. Defaults to UIModalTransitionStyleCoverVertical."

来自UIKit文档:“定义在模态显示时将用于此视图控制器的过渡样式。在要呈现的视图控制器上设置此属性,而不是演示者。默认为UIModalTransitionStyleCoverVertical。”

The viewDidLoad method will only be called after you already presented the view controller.

只有在您已经呈现视图控制器之后才会调用viewDidLoad方法。

The second problem was that you should use UIModalPresentationStyle.overCurrentContext.

第二个问题是你应该使用UIModalPresentationStyle.overCurrentContext。

#6


0  

The only way I able to get this to work was by doing this on the presenting view controller:

我能够让它工作的唯一方法是在呈现视图控制器上执行此操作:

    func didTapButton() {
    self.definesPresentationContext = true
    self.modalTransitionStyle = .crossDissolve
    let yourVC = self.storyboard?.instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
    let navController = UINavigationController(rootViewController: yourVC)
    navController.modalPresentationStyle = .overCurrentContext
    navController.modalTransitionStyle = .crossDissolve
    self.present(navController, animated: true, completion: nil)
}

#1


37  

First, remove all explicit setting of modal presentation style in code and do the following:

首先,删除代码中模态表示样式的所有显式设置,并执行以下操作:

  1. In the storyboard set the ModalViewController's modalPresentation style to Over Current context
  2. 在故事板中将ModalViewController的modalPresentation样式设置为Over Current上下文

如何在Swift中的当前视图上呈现模态

  1. Check the checkboxes in the Root/Presenting ViewController - Provide Context and Define Context. They seem to be working even unchecked.
  2. 检查Root / Presenting ViewController中的复选框 - 提供上下文和定义上下文。他们似乎工作甚至没有受到控制。

#2


12  

The only problem I can see in your code is that you are using CurrentContext instead of OverCurrentContext.

我在代码中可以看到的唯一问题是您使用的是CurrentContext而不是OverCurrentContext。

So, replace this:

所以,替换这个:

self.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
self.navigationController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext

for this:

为了这:

self.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
self.navigationController.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

#3


9  

You can try this code for Swift

您可以为Swift尝试此代码

 let popup : PopupVC = self.storyboard?.instantiateViewControllerWithIdentifier("PopupVC") as! PopupVC
 let navigationController = UINavigationController(rootViewController: popup)
 navigationController.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
 self.presentViewController(navigationController, animated: true, completion: nil)

#4


1  

I am updating a simple solution. First add an id to your segue which presents modal. Than in properties change it's presentation style to "Over Current Context". Than add this code in presenting view controller (The controller which is presenting modal).

我正在更新一个简单的解决方案首先在你的segue中添加一个id,它提供了模态。比在属性中将其呈现样式更改为“Over Current Context”。然后在呈现视图控制器(呈现模态的控制器)中添加此代码。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let Device = UIDevice.currentDevice()

    let iosVersion = NSString(string: Device.systemVersion).doubleValue

    let iOS8 = iosVersion >= 8
    let iOS7 = iosVersion >= 7 && iosVersion < 8
    if((segue.identifier == "chatTable")){

    if (iOS8){


          }
        else {

            self.navigationController?.modalPresentationStyle = UIModalPresentationStyle.CurrentContext


        }
    }
}

Make sure you change segue.identifier to your own id ;)

确保将segue.identifier更改为您自己的ID;)

#5


0  

The problem with setting the modalPresentationStyle from code was that you should have set it in the init() method of the presented view controller, not the parent view controller.

从代码设置modalPresentationStyle的问题是您应该在呈现的视图控制器的init()方法中设置它,而不是父视图控制器。

From UIKit docs: "Defines the transition style that will be used for this view controller when it is presented modally. Set this property on the view controller to be presented, not the presenter. Defaults to UIModalTransitionStyleCoverVertical."

来自UIKit文档:“定义在模态显示时将用于此视图控制器的过渡样式。在要呈现的视图控制器上设置此属性,而不是演示者。默认为UIModalTransitionStyleCoverVertical。”

The viewDidLoad method will only be called after you already presented the view controller.

只有在您已经呈现视图控制器之后才会调用viewDidLoad方法。

The second problem was that you should use UIModalPresentationStyle.overCurrentContext.

第二个问题是你应该使用UIModalPresentationStyle.overCurrentContext。

#6


0  

The only way I able to get this to work was by doing this on the presenting view controller:

我能够让它工作的唯一方法是在呈现视图控制器上执行此操作:

    func didTapButton() {
    self.definesPresentationContext = true
    self.modalTransitionStyle = .crossDissolve
    let yourVC = self.storyboard?.instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
    let navController = UINavigationController(rootViewController: yourVC)
    navController.modalPresentationStyle = .overCurrentContext
    navController.modalTransitionStyle = .crossDissolve
    self.present(navController, animated: true, completion: nil)
}