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

时间:2014-06-20 21:51:45

标签: xcode ipad modal-dialog swift transparent

(Xcode6,iOS8,Swift,iPad)

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

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

唯一的问题是,当模态变为全屏时,会删除呈现视图。 (参考Transparent Modal View on Navigation Controller)。

(这里的概念有点恼火。为什么删除底层视图?根据定义,模态出现在其他内容之上。一旦基础视图被删除,它就不再是一个模态了。它&& #39;介于模态转换和推送转换之间.Wa wa wa ...无论如何..)

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

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

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

tyvm ..更多信息如下。

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

enter image description here

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

enter image description here

7 个答案:

答案 0 :(得分:41)

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

  1. 在故事板中将ModalViewController的modalPresentation样式设置为Over Current context
  2. img1

    1. 选中Root / Presenting ViewController中的复选框 - Provide ContextDefine Context。 他们似乎工作甚至没有受到控制。

答案 1 :(得分:15)

您可以尝试使用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)

使用扩展程序

的swift 4最新语法
extension UIViewController {
    func presentOnRoot(`with` viewController : UIViewController){
        let navigationController = UINavigationController(rootViewController: viewController)
        navigationController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
        self.present(navigationController, animated: false, completion: nil)
    }
}

如何使用

let popup : PopupVC = self.storyboard?.instantiateViewControllerWithIdentifier("PopupVC") as! PopupVC
self.presentOnRoot(with: popup)

答案 2 :(得分:13)

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

所以,替换这个:

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

为此:

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

答案 3 :(得分:2)

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

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

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

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

答案 4 :(得分:2)

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

    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)
}

答案 5 :(得分:1)

我正在更新一个简单的解决方案。首先在你的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


        }
    }
}

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

答案 6 :(得分:1)

这在Swift 5.0中对我有用。在身份检查器中将情节提要ID设置为“ destinationVC”。

@IBAction func buttonTapped(_ sender: Any) {
    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let destVC = storyboard.instantiateViewController(withIdentifier: "destinationVC") as! MyViewController

    destVC.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
    destVC.modalTransitionStyle = UIModalTransitionStyle.crossDissolve

    self.present(destVC, animated: true, completion: nil)
}