UIModalPresentationStyle.CurrentContext Swift iOS 7

Sat*_*oup 11 objective-c uiviewcontroller ios uimodalpresentationstyle swift

我想PresentedView在另一个视图中显示一个视图 - Background View使用iOS 7.在我的应用程序中,我正在使用UITabBArController,因此在运行时我不知道哪个视图将是背景视图(可能是任何标签栏项目).以下是结构:

UITabBarController
  ---> TabItem1 - FirstUIViewController
  ---> TabItem2 - SecondUIViewController
  ---> TabItem3 - ThirdUIViewController
Run Code Online (Sandbox Code Playgroud)

需要这样的东西:

在此输入图像描述

当应用程序加载时,我正在运行TabItem1 - FirstUIViewController.当我点击时TabItem3,我想要ThirdUIViewController出现在Top on上FirstUIViewController,'FirstUIViewController'应该出现在后台,没有启用用户交互.

到目前为止我做了什么:

  1. 因为,UIViewControllers被添加Relationship Controllers到出现TabBar Item在`的UITabBarController,我添加了一个SEGUE从tabbarcontroller到ThridViewController.

  2. 将此Segue的PresentationStyle更改为UIModalPresentationStyle.CurrentContext,并进行了以下修改

    func `viewDidLoad()` {
        super.viewDidLoad()
        self.performSegue("identifier", sender: self)
    }
    
    Run Code Online (Sandbox Code Playgroud)

什么都没发生,我只看到带有白色背景的'ThridViewController'

  1. 我试过手动编码的方法:

    func `viewDidLoad()` {
        super.viewDidLoad()
        let overlayController:UIThirdViewController = UIThirdViewController() //This controller has a view added on top of it, which covers top half screen only
        overlayController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
        self.presentViewController(overlayController, animated: true, completion: nil)
    }
    
    Run Code Online (Sandbox Code Playgroud)

没变.新视图会覆盖previos视图.如果我添加这个overlayController.view.backgroundColor = UIColor.clearColor(),我看到半屏黑色和一半包含我的新视图

问题点:

  1. 我应该在哪里编写代码来初始化/调用ThirdViewController以显示在当前视图的顶部?
  2. 如何修复黑屏问题并使其在iOS 7上运行?

我正在使用Xcode 7并在iOS7上运行.请帮忙.工作代码片段将不胜感激.不要将其他堆栈溢出帖子作为答案发布,除非代码有效并且您已经尝试过自己.

更新: - 通过这种方法,我得到一个黑屏

class TabBarViewController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
    }

    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

        let switchController:UIViewController = SwitchViewController()

        self.presentingViewController?.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
        self.presentingViewController?.view.backgroundColor = UIColor.clearColor()
        self.presentViewController(switchController, animated: true, completion: nil)
        return false
    }
}
Run Code Online (Sandbox Code Playgroud)

bob*_*ics 3

使其成为自定义容器视图控制器(Apple 文档)。工作示例代码:

class MyTabVC: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
    }

    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
        presentThirdVC()
        return false
    }

    func presentThirdVC() {
        let myThirdVC = MyThirdVC.makeVC()

        myThirdVC.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.1)
        addChildViewController(myThirdVC)
        var newFrame = CGRectInset(view.bounds, 0, 50) // hack, do what you want
        myThirdVC.view.frame = newFrame
        view.addSubview(myThirdVC.view)
        didMoveToParentViewController(myThirdVC)
    }

}

class MyThirdVC: UIViewController {
    class func makeVC() -> MyThirdVC {
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("myThirdVC") as! MyThirdVC
    }
}
Run Code Online (Sandbox Code Playgroud)

截屏:

在此输入图像描述