pmi*_*ick 8 cocoa-touch uikit ios swift
我遇到了视图控制器包含问题,并希望使用自定义演示文稿/动画呈现视图控制器"当前上下文".
我有一个根视图控制器,它有两个子视图控制器,可以作为子项添加到根目录中.当这些子视图控制器呈现视图控制器时,我希望该呈现是在当前上下文中,以便当从视图中移除所呈现的子项时,所述呈现的模式也将被移除.此外,如果子A呈现视图控制器,我会期望子B的'presentViewController'属性在"当前上下文"演示中为零,即使A仍然呈现.
当我将modalPresentationStyle我呈现的视图控制器设置为overCurrentContext,并且子视图控制器definesPresentationContext设置为true 时,一切都按预期工作.
如果我已经modalPresentationStyle设置custom并覆盖shouldPresentInFullscreen在我的自定义演示控制器中返回false,那么当我期望它时,这不起作用.
这是一个说明问题的示例:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
如果您将setup()功能更改ProgressController为:
private func setup() {
modalPresentationStyle = .overCurrentContext
modalTransitionStyle = .crossDissolve
transitioningDelegate = progressTransitioningDelegate
}
Run Code Online (Sandbox Code Playgroud)
演示文稿/视图层次结构将按预期运行,但不会使用自定义演示文稿.
Apple的文档shouldPresentInFullscreen似乎表明这应该有效:
此方法的默认实现返回true,表示演示文稿覆盖整个屏幕.您可以覆盖此方法并返回false以强制演示文稿仅在当前上下文中显示.
如果重写此方法,请不要调用super.
我在iOS 10的Xcode 8和iOS 11的Xcode 9上进行了测试,上述代码在任何一种情况下都无法正常工作.
我猜你已经发现了一个错误。文档说shouldPresentInFullscreen可以将其变成currentContext演示文稿,但它没有任何作用。(除了你的测试和我的测试之外,我还发现了一些网上对此的抱怨,让我认为情况确实如此。)
结论是,如果您使用 的表示样式,则无法获得默认currentContext行为(运行时查阅源视图控制器并向上查找层次结构) 。definesPresentationContext.custom
我建议向 Apple 提交错误。