ios - 如何使用自定义转换在当前上下文中呈现 View Controller ?

标签 ios swift cocoa-touch uikit

我遇到了 View Controller 包含的问题,我想通过自定义演示/动画“在当前上下文中”呈现 View Controller 。

我有一个 Root View Controller ,它有两个 subview Controller ,它们可以作为根的 subview Controller 添加和删除。当这些 subview Controller 呈现一个 View Controller 时,我希望呈现在当前上下文之上,以便当呈现的 subview 从 View 层次结构中移除并释放时,呈现的模态也将被移除。此外,如果 child A 呈现一个 View Controller ,我希望 child B 的“presentedViewController”属性在“当前上下文”演示中为 nil,即使 A 仍在呈现。

当我将呈现的 View Controller 的 modalPresentationStyle 设置为 overCurrentContext 并且 subview Controller 设置了 definesPresentationContext 时,一切都按预期工作为真。

如果我将 modalPresentationStyle 设置为 custom 并覆盖 shouldPresentInFullscreen 在我的返回 false自定义呈现 Controller 。

这是一个说明问题的例子:

import UIKit

final class ProgressController: UIViewController {
    private lazy var activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .white)
    private lazy var progressTransitioningDelegate = ProgressTransitioningDelegate()

    // MARK: Lifecycle

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

        setup()
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setup()
    }

    override public func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor(white: 0, alpha: 0)

        view.addSubview(activityIndicatorView)
        activityIndicatorView.startAnimating()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }

    override public func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        activityIndicatorView.center = CGPoint(x: view.bounds.width/2, y: view.bounds.height/2)
    }

    // MARK: Private

    private func setup() {
        modalPresentationStyle = .custom
        modalTransitionStyle = .crossDissolve
        transitioningDelegate = progressTransitioningDelegate
    }
}

final class ProgressTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return DimBackgroundPresentationController(presentedViewController: presented, presenting: source)
    }
}

final class DimBackgroundPresentationController: UIPresentationController {
    lazy var overlayView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(white: 0, alpha: 0.5)
        return v
    }()

    override var shouldPresentInFullscreen: Bool {
        return false
    }

    override func presentationTransitionWillBegin() {
        super.presentationTransitionWillBegin()

        overlayView.alpha = 0
        containerView!.addSubview(overlayView)
        containerView!.addSubview(presentedView!)

        if let coordinator = presentedViewController.transitionCoordinator {
            coordinator.animate(alongsideTransition: { _ in
                self.overlayView.alpha = 1
            }, completion: nil)
        }
    }

    override func containerViewDidLayoutSubviews() {
        super.containerViewDidLayoutSubviews()

        overlayView.frame = presentingViewController.view.bounds
    }

    override func dismissalTransitionWillBegin() {
        super.dismissalTransitionWillBegin()

        let coordinator = presentedViewController.transitionCoordinator
        coordinator?.animate(alongsideTransition: { _ in
            self.overlayView.alpha = 0
        }, completion: nil)
    }
}

class ViewControllerA: UIViewController {
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        definesPresentationContext = true

        let vc = ProgressController()
        self.present(vc, animated: true) {
        }
    }
}

class ViewController: UIViewController {

    let container = UIScrollView()

    override func viewDidLoad() {
        super.viewDidLoad()

        container.frame = view.bounds
        view.addSubview(container)

        let lhs = ViewControllerA()
        lhs.view.backgroundColor = .red

        let rhs = UIViewController()
        rhs.view.backgroundColor = .blue

        addChildViewController(lhs)
        lhs.view.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
        container.addSubview(lhs.view)
        lhs.didMove(toParentViewController: self)

        addChildViewController(rhs)
        rhs.view.frame = CGRect(x: view.bounds.width, y: 0, width: view.bounds.width, height: view.bounds.height)
        container.addSubview(rhs.view)
        rhs.didMove(toParentViewController: self)


        container.contentSize = CGSize(width: view.bounds.width * 2, height: view.bounds.height)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
//        let rect = CGRect(x: floor(view.bounds.width/2.0), y: 0, width: view.bounds.width, height: view.bounds.height)
//        container.scrollRectToVisible(rect, animated: true)
    }
}
  1. 这将显示一个包含两个 subview Controller 的 ScrollView 的 ViewController
  2. 左侧的红色 View Controller 将显示一个进度 View Controller ,该 Controller 应显示在当前上下文中。
  3. 如果 View Controller “在当前上下文中”正确呈现,那么您将能够滚动 ScrollView ,如果您检查了蓝色右侧 View Controller 的“presentedViewController”属性,那么它应该为 nil。这些都不是真的。

如果将 ProgressController 上的 setup() 函数更改为:

private func setup() {
    modalPresentationStyle = .overCurrentContext
    modalTransitionStyle = .crossDissolve
    transitioningDelegate = progressTransitioningDelegate
}

表示/ View 层次结构将按预期运行,但不会使用自定义表示。

Apple 的 shouldPresentInFullscreen 文档似乎表明这应该有效:

The default implementation of this method returns true, indicating that the presentation covers the entire screen. You can override this method and return false to force the presentation to display only in the current context.

If you override this method, do not call super.

我在 iOS 10 上的 Xcode 8 和 iOS 11 上的 Xcode 9 中对此进行了测试,上面的代码在任何一种情况下都无法按预期工作。

最佳答案

我猜你发现了一个错误。文档说 shouldPresentInFullscreen 可以将其转换为 currentContext 演示文稿,但它什么也没做。 (除了你的测试和我的测试外,我还发现了一些网上对此的投诉,导致我认为是这样。)

结论是,如果您使用演示风格,您将无法获得默认的 currentContext 行为(其中运行时查询源 View Controller 并在层次结构中向上查找 definesPresentationContext) .custom.

我建议向 Apple 提交错误。

关于ios - 如何使用自定义转换在当前上下文中呈现 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45966293/

相关文章:

ios - 将 TableView 数组拆分为标签和副标题

ios - UISearchController:即使搜索栏为空也显示结果

ios - 无法将框架/Pod 导入 xcode

ios - Swift 3 上下文类型 'AnyObject' 不能与字典文字一起使用

ios - 在不同的 View Controller 中打开webkit View

ios - 如何在ios的uipageviewcontroller单击时禁用页面 curl

objective-c - iOS 超时功能无法正常工作

ios - 将托管对象从私有(private)上下文传递到主上下文 : it looses its related objects

ios - 使用 iOS 7 向后边缘滑动时在哪里实现自动保存代码?

ios - 使用动态高度和全高创建不同的单元尺寸