当视图控制器在本地显示并显示警报时,控件不会变暗
Controls not dimming when view controller is presented locally and alert displayed
当我在视图控制器上显示 UIAlertController
时,控件按预期变暗。
但是,如果同一视图控制器本身以模态方式呈现,则警报控制器的显示不会使控件变暗(两个按钮保持蓝色)。
如何让呈现的视图控制器本身正确地处理呈现,并调暗它的控件?
Here 是一个小示例项目。相关代码在MainViewController.swift
.
到目前为止,我最好的解决方法是使用自定义的 UIAlertController
子类来设置 tintAdjustmentMode
及其 appear/disappear 动画,使用 transitionCoordinator
:
/// A `UIAlertController` that can udpates its presenting view controller's `tintAdjustmentMode` code as it appears and disappears
class TintAdjustingAlertController: UIAlertController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
animatePresentingViewTintAdjustmentMode(tintAdjustmentMode: .dimmed, forViewControllerAtKey: .from)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
animatePresentingViewTintAdjustmentMode(tintAdjustmentMode: .automatic, forViewControllerAtKey: .to)
}
private func animatePresentingViewTintAdjustmentMode(tintAdjustmentMode mode: UIView.TintAdjustmentMode, forViewControllerAtKey key: UITransitionContextViewControllerKey) {
transitionCoordinator?.animate(alongsideTransition: { context in
if let presentingNavigationController = context.viewController(forKey: key) as? UINavigationController {
presentingNavigationController.navigationBar.tintAdjustmentMode = mode
presentingNavigationController.viewControllers.forEach { [=10=].view.tintAdjustmentMode = mode }
} else if let presentingViewController = context.viewController(forKey: key) {
presentingViewController.view.tintAdjustmentMode = mode
}
}, completion: nil)
}
}
这行得通,但我希望不必在我的代码中使用它。仍然很想知道 a) 是否有一种简单的方法可以使这项工作按预期进行,或者 b) 如果这确实是一个 iOS 错误,是否有更优雅的解决方法?
当我在视图控制器上显示 UIAlertController
时,控件按预期变暗。
但是,如果同一视图控制器本身以模态方式呈现,则警报控制器的显示不会使控件变暗(两个按钮保持蓝色)。
如何让呈现的视图控制器本身正确地处理呈现,并调暗它的控件?
Here 是一个小示例项目。相关代码在MainViewController.swift
.
到目前为止,我最好的解决方法是使用自定义的 UIAlertController
子类来设置 tintAdjustmentMode
及其 appear/disappear 动画,使用 transitionCoordinator
:
/// A `UIAlertController` that can udpates its presenting view controller's `tintAdjustmentMode` code as it appears and disappears
class TintAdjustingAlertController: UIAlertController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
animatePresentingViewTintAdjustmentMode(tintAdjustmentMode: .dimmed, forViewControllerAtKey: .from)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
animatePresentingViewTintAdjustmentMode(tintAdjustmentMode: .automatic, forViewControllerAtKey: .to)
}
private func animatePresentingViewTintAdjustmentMode(tintAdjustmentMode mode: UIView.TintAdjustmentMode, forViewControllerAtKey key: UITransitionContextViewControllerKey) {
transitionCoordinator?.animate(alongsideTransition: { context in
if let presentingNavigationController = context.viewController(forKey: key) as? UINavigationController {
presentingNavigationController.navigationBar.tintAdjustmentMode = mode
presentingNavigationController.viewControllers.forEach { [=10=].view.tintAdjustmentMode = mode }
} else if let presentingViewController = context.viewController(forKey: key) {
presentingViewController.view.tintAdjustmentMode = mode
}
}, completion: nil)
}
}
这行得通,但我希望不必在我的代码中使用它。仍然很想知道 a) 是否有一种简单的方法可以使这项工作按预期进行,或者 b) 如果这确实是一个 iOS 错误,是否有更优雅的解决方法?