gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:52:20 24 4
gpt4 key购买 nike

我遇到了 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com